| 1 |
|
| 2 |
|
| 3 |
package main |
| 4 |
|
| 5 |
import ( |
| 6 |
"encoding/hex" |
| 7 |
"fmt" |
| 8 |
"log" |
| 9 |
"net/http" |
| 10 |
"os" |
| 11 |
|
| 12 |
|
| 13 |
"gopkg.in/jcmturner/gokrb5.v5/credentials" |
| 14 |
"gopkg.in/jcmturner/gokrb5.v5/keytab" |
| 15 |
"gopkg.in/jcmturner/gokrb5.v5/service" |
| 16 |
"gopkg.in/jcmturner/gokrb5.v5/testdata" |
| 17 |
) |
| 18 |
|
| 19 |
const ( |
| 20 |
port = ":9080" |
| 21 |
) |
| 22 |
|
| 23 |
func main() { |
| 24 |
|
| 25 |
|
| 26 |
l := log.New(os.Stderr, "GOKRB5 Service: ", log.Ldate|log.Ltime|log.Lshortfile) |
| 27 |
|
| 28 |
|
| 29 |
b, _ := hex.DecodeString(testdata.HTTP_KEYTAB) |
| 30 |
kt, _ := keytab.Parse(b) |
| 31 |
|
| 32 |
|
| 33 |
th := http.HandlerFunc(testAppHandler) |
| 34 |
|
| 35 |
|
| 36 |
mux := http.NewServeMux() |
| 37 |
mux.Handle("/", service.SPNEGOKRB5Authenticate(th, kt, "", false, l)) |
| 38 |
|
| 39 |
|
| 40 |
log.Fatal(http.ListenAndServe(port, mux)) |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
func testAppHandler(w http.ResponseWriter, r *http.Request) { |
| 45 |
w.WriteHeader(http.StatusOK) |
| 46 |
ctx := r.Context() |
| 47 |
creds := ctx.Value(service.CTXKeyCredentials).(credentials.Credentials) |
| 48 |
fmt.Fprintf(w, |
| 49 |
`<html> |
| 50 |
<h1>GOKRB5 Handler</h1> |
| 51 |
<ul> |
| 52 |
<li>Authenticed user: %s</li> |
| 53 |
<li>User's realm: %s</li> |
| 54 |
<li>Authn time: %v</li> |
| 55 |
<li>Session ID: %s</li> |
| 56 |
<ul> |
| 57 |
</html>`, |
| 58 |
creds.UserName(), |
| 59 |
creds.Domain(), |
| 60 |
creds.AuthTime(), |
| 61 |
creds.SessionID(), |
| 62 |
) |
| 63 |
return |
| 64 |
} |