Просмотр исходного кода

fixes for documentation and constants for map keys

Jonathan Turner 9 лет назад
Родитель
Сommit
4a426de5c9
6 измененных файлов с 21 добавлено и 16 удалено
  1. 9 8
      README.md
  2. 4 0
      credentials/credentials.go
  3. 3 3
      examples/example-AD.go
  4. 2 2
      examples/example.go
  5. 1 1
      service/APExchange.go
  6. 2 2
      service/http.go

+ 9 - 8
README.md

@@ -80,10 +80,11 @@ cl.EnableAutoSessionRenewal()
 #### Authenticate to a Service
 
 ##### HTTP SPNEGO
-Create the HTTP request object and then call the client's SetSPNEGOHeader method passing the Service Principal Name (SPN)
+Create the HTTP request object and then call the client's SetSPNEGOHeader method passing the Service Principal Name (SPN) or to auto generate the SPN from the request object pass a null string ""
 ```go
 r, _ := http.NewRequest("GET", "http://host.test.gokrb5/index.html", nil)
-cl.SetSPNEGOHeader(r, "")
+spn := ""
+cl.SetSPNEGOHeader(r, spn)
 HTTPResp, err := http.DefaultClient.Do(r)
 ```
 
@@ -146,15 +147,15 @@ http.Handler("/", service.SPNEGOKRB5Authenticate(h, kt, serivceAccountName, l))
 The serviceAccountName needs to be defined when using Active Directory where the SPN is mapped to a user account.
 If this is not required it should be set to an empty string "".
 If authentication succeeds then the request's context will have the following values added so they can be accessed within the application's handler:
-* "authenticated" - Boolean indicating if the user is authenticated. Use of this value should also handle that this value may not be set and should assume "false" in that case.
-* "credentials" - The authenticated user's credentials.
-If Microsoft Active Directory is used as the KDC then additional ADCredentials are available. For example the SIDs of the users group membership are available and can be used by your application for authorization.
+* service.CTXKey_Authenticated - Boolean indicating if the user is authenticated. Use of this value should also handle that this value may not be set and should assume "false" in that case.
+* service.CTXKey_Credentials - The authenticated user's credentials.
+If Microsoft Active Directory is used as the KDC then additional ADCredentials are available in the credentials.Attributes map under the key credentials.AttributeKey_ADCredentials. For example the SIDs of the users group membership are available and can be used by your application for authorization.
 Access the credentials within your application:
 ```go
 ctx := r.Context()
-if validuser, ok := ctx.Value("authenticated").(bool); ok && validuser {
-        if creds, ok := ctx.Value("credentials").(credentials.Credentials); ok {
-                if ADCreds, ok := creds.Attributes["ADCredentials"].(credentials.ADCredentials); ok {
+if validuser, ok := ctx.Value(service.CTXKey_Authenticated).(bool); ok && validuser {
+        if creds, ok := ctx.Value(service.CTXKey_Credentials).(credentials.Credentials); ok {
+                if ADCreds, ok := creds.Attributes[credentials.AttributeKey_ADCredentials].(credentials.ADCredentials); ok {
                         // Now access the fields of the ADCredentials struct. For example:
                         groupSids := ADCreds.GroupMembershipSIDs
                 }

+ 4 - 0
credentials/credentials.go

@@ -8,6 +8,10 @@ import (
 	"time"
 )
 
+const (
+	AttributeKey_ADCredentials = 1
+)
+
 // Credentials struct for a user.
 // Contains either a keytab, password or both.
 // Keytabs are used over passwords if both are defined.

+ 3 - 3
examples/example-AD.go

@@ -73,11 +73,11 @@ func httpServer() *httptest.Server {
 func testAppHandler(w http.ResponseWriter, r *http.Request) {
 	ctx := r.Context()
 	fmt.Fprint(w, "<html>\n<p><h1>TEST.GOKRB5 Handler</h1></p>\n")
-	if validuser, ok := ctx.Value(service.AUTHENTICATED_CTXKEY).(bool); ok && validuser {
-		if creds, ok := ctx.Value(service.CREDENTIALS_CTXKEY).(credentials.Credentials); ok {
+	if validuser, ok := ctx.Value(service.CTXKey_Authenticated).(bool); ok && validuser {
+		if creds, ok := ctx.Value(service.CTXKey_Credentials).(credentials.Credentials); ok {
 			fmt.Fprintf(w, "<ul><li>Authenticed user: %s</li>\n", creds.Username)
 			fmt.Fprintf(w, "<li>User's realm: %s</li>\n", creds.Realm)
-			if ADCreds, ok := creds.Attributes["ADCredentials"].(credentials.ADCredentials); ok {
+			if ADCreds, ok := creds.Attributes[credentials.AttributeKey_ADCredentials].(credentials.ADCredentials); ok {
 				// Now access the fields of the ADCredentials struct. For example:
 				fmt.Fprintf(w, "<li>EffectiveName: %v</li>\n", ADCreds.EffectiveName)
 				fmt.Fprintf(w, "<li>FullName: %v</li>\n", ADCreds.FullName)

+ 2 - 2
examples/example.go

@@ -86,8 +86,8 @@ func httpServer() *httptest.Server {
 func testAppHandler(w http.ResponseWriter, r *http.Request) {
 	ctx := r.Context()
 	fmt.Fprint(w, "<html>\n<p><h1>TEST.GOKRB5 Handler</h1></p>\n")
-	if validuser, ok := ctx.Value(service.AUTHENTICATED_CTXKEY).(bool); ok && validuser {
-		if creds, ok := ctx.Value(service.CREDENTIALS_CTXKEY).(credentials.Credentials); ok {
+	if validuser, ok := ctx.Value(service.CTXKey_Authenticated).(bool); ok && validuser {
+		if creds, ok := ctx.Value(service.CTXKey_Credentials).(credentials.Credentials); ok {
 			fmt.Fprintf(w, "<ul><li>Authenticed user: %s</li>\n", creds.Username)
 			fmt.Fprintf(w, "<li>User's realm: %s</li></ul>\n", creds.Realm)
 		}

+ 1 - 1
service/APExchange.go

@@ -88,7 +88,7 @@ func ValidateAPREQ(APReq messages.APReq, kt keytab.Keytab, sa string, cAddr stri
 	}
 	if isPAC {
 		// There is a valid PAC. Adding attributes to creds
-		creds.Attributes["ADCredentials"] = credentials.ADCredentials{
+		creds.Attributes[credentials.AttributeKey_ADCredentials] = credentials.ADCredentials{
 			GroupMembershipSIDs: pac.KerbValidationInfo.GetGroupMembershipSIDs(),
 			LogOnTime:           pac.KerbValidationInfo.LogOnTime.Time(),
 			LogOffTime:          pac.KerbValidationInfo.LogOffTime.Time(),

+ 2 - 2
service/http.go

@@ -18,8 +18,8 @@ const (
 	SPNEGO_NegTokenResp_Krb_Accept_Completed = "Negotiate oRQwEqADCgEAoQsGCSqGSIb3EgECAg=="
 	// SPNEGO_NegTokenResp_Reject - pThe response on a failed authentication always has this rejection header. Capturing as const so we don't have marshaling and encoding overhead.
 	SPNEGO_NegTokenResp_Reject        = "Negotiate oQcwBaADCgEC"
-	CTXKey_Credentials         ctxKey = 0
-	CTXKey_Authenticated       ctxKey = 1
+	CTXKey_Authenticated       ctxKey = 0
+	CTXKey_Credentials         ctxKey = 1
 )
 
 // SPNEGOKRB5Authenticate is a Kerberos SPNEGO authentication HTTP handler wrapper.