| 1 |
|
| 2 |
|
| 3 |
package main |
| 4 |
|
| 5 |
import ( |
| 6 |
"encoding/hex" |
| 7 |
"fmt" |
| 8 |
"gopkg.in/jcmturner/gokrb5.v5/client" |
| 9 |
"gopkg.in/jcmturner/gokrb5.v5/config" |
| 10 |
"gopkg.in/jcmturner/gokrb5.v5/credentials" |
| 11 |
"gopkg.in/jcmturner/gokrb5.v5/keytab" |
| 12 |
"gopkg.in/jcmturner/gokrb5.v5/service" |
| 13 |
"gopkg.in/jcmturner/gokrb5.v5/testdata" |
| 14 |
"io/ioutil" |
| 15 |
"log" |
| 16 |
"net/http" |
| 17 |
"net/http/httptest" |
| 18 |
"os" |
| 19 |
) |
| 20 |
|
| 21 |
func main() { |
| 22 |
s := httpServer() |
| 23 |
defer s.Close() |
| 24 |
|
| 25 |
b, _ := hex.DecodeString(testdata.TESTUSER1_KEYTAB) |
| 26 |
kt, _ := keytab.Parse(b) |
| 27 |
c, _ := config.NewConfigFromString(testdata.TEST_KRB5CONF) |
| 28 |
cl := client.NewClientWithKeytab("testuser1", "USER.GOKRB5", kt) |
| 29 |
cl.WithConfig(c) |
| 30 |
httpRequest(s.URL, cl) |
| 31 |
|
| 32 |
b, _ = hex.DecodeString(testdata.TESTUSER2_KEYTAB) |
| 33 |
kt, _ = keytab.Parse(b) |
| 34 |
c, _ = config.NewConfigFromString(testdata.TEST_KRB5CONF) |
| 35 |
cl = client.NewClientWithKeytab("testuser2", "USER.GOKRB5", kt) |
| 36 |
cl.WithConfig(c) |
| 37 |
httpRequest(s.URL, cl) |
| 38 |
|
| 39 |
|
| 40 |
} |
| 41 |
|
| 42 |
func httpRequest(url string, cl client.Client) { |
| 43 |
l := log.New(os.Stderr, "GOKRB5 Client: ", log.Ldate|log.Ltime|log.Lshortfile) |
| 44 |
|
| 45 |
err := cl.Login() |
| 46 |
if err != nil { |
| 47 |
l.Printf("Error on AS_REQ: %v\n", err) |
| 48 |
} |
| 49 |
r, _ := http.NewRequest("GET", url, nil) |
| 50 |
err = cl.SetSPNEGOHeader(r, "HTTP/host.res.gokrb5") |
| 51 |
if err != nil { |
| 52 |
l.Printf("Error setting client SPNEGO header: %v", err) |
| 53 |
} |
| 54 |
httpResp, err := http.DefaultClient.Do(r) |
| 55 |
if err != nil { |
| 56 |
l.Printf("Request error: %v\n", err) |
| 57 |
} |
| 58 |
fmt.Fprintf(os.Stdout, "Response Code: %v\n", httpResp.StatusCode) |
| 59 |
content, _ := ioutil.ReadAll(httpResp.Body) |
| 60 |
fmt.Fprintf(os.Stdout, "Response Body:\n%s\n", content) |
| 61 |
} |
| 62 |
|
| 63 |
func httpServer() *httptest.Server { |
| 64 |
l := log.New(os.Stderr, "GOKRB5 Service: ", log.Ldate|log.Ltime|log.Lshortfile) |
| 65 |
b, _ := hex.DecodeString(testdata.SYSHTTP_KEYTAB) |
| 66 |
kt, _ := keytab.Parse(b) |
| 67 |
th := http.HandlerFunc(testAppHandler) |
| 68 |
s := httptest.NewServer(service.SPNEGOKRB5Authenticate(th, kt, "sysHTTP", false, l)) |
| 69 |
return s |
| 70 |
} |
| 71 |
|
| 72 |
func testAppHandler(w http.ResponseWriter, r *http.Request) { |
| 73 |
ctx := r.Context() |
| 74 |
fmt.Fprint(w, "<html>\n<p><h1>TEST.GOKRB5 Handler</h1></p>\n") |
| 75 |
if validuser, ok := ctx.Value(service.CTXKeyAuthenticated).(bool); ok && validuser { |
| 76 |
if creds, ok := ctx.Value(service.CTXKeyCredentials).(credentials.Credentials); ok { |
| 77 |
fmt.Fprintf(w, "<ul><li>Authenticed user: %s</li>\n", creds.UserName()) |
| 78 |
fmt.Fprintf(w, "<li>User's realm: %s</li>\n", creds.Domain()) |
| 79 |
fmt.Fprint(w, "<li>Authz Attributes (Group Memberships):</li><ul>\n") |
| 80 |
for _, s := range creds.AuthzAttributes() { |
| 81 |
fmt.Fprintf(w, "<li>%v</li>\n", s) |
| 82 |
} |
| 83 |
fmt.Fprint(w, "</ul>\n") |
| 84 |
if ADCreds, ok := creds.Attributes[credentials.AttributeKeyADCredentials].(credentials.ADCredentials); ok { |
| 85 |
|
| 86 |
fmt.Fprintf(w, "<li>EffectiveName: %v</li>\n", ADCreds.EffectiveName) |
| 87 |
fmt.Fprintf(w, "<li>FullName: %v</li>\n", ADCreds.FullName) |
| 88 |
fmt.Fprintf(w, "<li>UserID: %v</li>\n", ADCreds.UserID) |
| 89 |
fmt.Fprintf(w, "<li>PrimaryGroupID: %v</li>\n", ADCreds.PrimaryGroupID) |
| 90 |
fmt.Fprintf(w, "<li>Group SIDs: %v</li>\n", ADCreds.GroupMembershipSIDs) |
| 91 |
fmt.Fprintf(w, "<li>LogOnTime: %v</li>\n", ADCreds.LogOnTime) |
| 92 |
fmt.Fprintf(w, "<li>LogOffTime: %v</li>\n", ADCreds.LogOffTime) |
| 93 |
fmt.Fprintf(w, "<li>PasswordLastSet: %v</li>\n", ADCreds.PasswordLastSet) |
| 94 |
fmt.Fprintf(w, "<li>LogonServer: %v</li>\n", ADCreds.LogonServer) |
| 95 |
fmt.Fprintf(w, "<li>LogonDomainName: %v</li>\n", ADCreds.LogonDomainName) |
| 96 |
fmt.Fprintf(w, "<li>LogonDomainID: %v</li>\n", ADCreds.LogonDomainID) |
| 97 |
} |
| 98 |
fmt.Fprint(w, "</ul>") |
| 99 |
} |
| 100 |
|
| 101 |
} else { |
| 102 |
w.WriteHeader(http.StatusUnauthorized) |
| 103 |
fmt.Fprint(w, "Authentication failed") |
| 104 |
} |
| 105 |
fmt.Fprint(w, "</html>") |
| 106 |
return |
| 107 |
} |