-
Notifications
You must be signed in to change notification settings - Fork 29
/
main.go
98 lines (85 loc) · 3.17 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"crypto/rsa"
"fmt"
jwt "github.com/dgrijalva/jwt-go"
"github.com/gorilla/mux"
"github.com/samitpal/simple-sso/util"
"io/ioutil"
"log"
"net/http"
"strings"
)
var parsedPubKey *rsa.PublicKey
func init() {
key, _ := ioutil.ReadFile("../key_pair/demo.rsa.pub") // this is the public key of the login (simple-sso) server
parsedPubKey, _ = jwt.ParseRSAPublicKeyFromPEM(key)
}
func cookieCheck(w http.ResponseWriter, r *http.Request) {
c, err := r.Cookie("SSO_C")
if err == http.ErrNoCookie {
// we redirect to the login service setting the appropriate s_url to come back after auth.
http.Redirect(w, r, "https://127.0.0.1:8081/sso?s_url=https://127.0.0.1:8082/cookie", 301)
return
}
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
parts := strings.Split(strings.Split(c.String(), "=")[1], ".")
err = jwt.SigningMethodRS512.Verify(strings.Join(parts[0:2], "."), parts[2], parsedPubKey)
if err != nil {
log.Fatalf("[%v] Error while verifying key: %v", strings.Split(c.String(), "=")[1], err)
}
tokenString := strings.Split(c.String(), "=")[1]
token, err := jwt.ParseWithClaims(tokenString, &util.CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
return parsedPubKey, nil
})
claims, ok := token.Claims.(*util.CustomClaims) // claims.User and claims.Roles are what we are interested in.
if ok && token.Valid {
fmt.Printf("User: %v Roles: %v Tok_Expires: %v \n", claims.User, claims.Roles, claims.StandardClaims.ExpiresAt)
} else {
fmt.Println(err)
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "You have visited a cookietest page.\n\n")
fmt.Fprintf(w, "User: %v, Roles: %v, Tok_Expires: %v\n", claims.User, claims.Roles, claims.StandardClaims.ExpiresAt)
return
}
func authTokCheck(w http.ResponseWriter, r *http.Request) {
h := r.Header.Get("Authorization")
if h == "" {
http.Error(w, "No Authorization header", http.StatusInternalServerError)
return
}
parts := strings.Split(strings.Split(h, " ")[1], ".")
err := jwt.SigningMethodRS512.Verify(strings.Join(parts[0:2], "."), parts[2], parsedPubKey)
if err != nil {
log.Fatalf("[%v] Error while verifying key: %v", strings.Split(h, "=")[1], err)
}
tokenString := strings.Split(h, " ")[1]
token, err := jwt.ParseWithClaims(tokenString, &util.CustomClaims{}, func(token *jwt.Token) (interface{}, error) {
return parsedPubKey, nil
})
claims, ok := token.Claims.(*util.CustomClaims) // claims.User and claims.Roles are what we are interested in.
if ok && token.Valid {
fmt.Printf("User: %v Roles: %v Tok_Expires: %v \n", claims.User, claims.Roles, claims.StandardClaims.ExpiresAt)
} else {
fmt.Println(err)
}
w.WriteHeader(http.StatusOK)
fmt.Fprintf(w, "You have visited a auth tokentest page.\n\n")
fmt.Fprintf(w, "User: %v, Roles: %v, Tok_Expires: %v\n", claims.User, claims.Roles, claims.StandardClaims.ExpiresAt)
return
}
func main() {
log.Println("Starting app server.")
r := mux.NewRouter()
r.HandleFunc("/cookie", cookieCheck)
r.HandleFunc("/auth_token", authTokCheck)
http.Handle("/", r)
err := http.ListenAndServeTLS(":8082", "../ssl_certs/cert.pem", "../ssl_certs/key.pem", nil)
if err != nil {
log.Fatal("ListenAndServe: ", err)
}
}