forked from jchannon/AzureSecuredAPIWithOT
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (78 loc) · 2.27 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 (
"AzureSecuredAPIWithOT/configs"
"AzureSecuredAPIWithOT/helpers/pages"
"AzureSecuredAPIWithOT/logger"
"crypto/rsa"
"fmt"
"github.com/golang-jwt/jwt"
"github.com/lestrrat-go/jwx/jwa"
"github.com/lestrrat-go/jwx/jwk"
"github.com/spf13/viper"
"log"
"net/http"
)
func main() {
configs.InitializeViper()
logger.InitializeZapCustomLogger()
InitializeOAuthMicrosoft()
// Routes for the application
http.HandleFunc("/", HandleMain)
http.HandleFunc("/login-ms", HandleMicrosoftLogin)
http.HandleFunc("/callback-ms", CallBackFromMicrosoft)
http.HandleFunc("/protected-ms", middleware(ProtectedRoute))
http.HandleFunc("/logout-ms", LogoutRoute)
logger.Log.Info("Started running on http://localhost:" + viper.GetString("port"))
log.Fatal(http.ListenAndServe(":"+viper.GetString("port"), nil))
}
func HandleMain(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
w.WriteHeader(http.StatusOK)
w.Write([]byte(pages.IndexPage))
}
func middleware(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
_, err := verifyToken(r)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(pages.UnAuthorizedPage))
return
}
next(w, r)
}
}
func extractToken(r *http.Request) string {
accessCookie, err := r.Cookie("access_token")
if err != nil {
return ""
}
bearToken := accessCookie.Value
return bearToken
}
func verifyToken(r *http.Request) (*jwt.Token, error) {
tokenString := extractToken(r)
keySet, err := jwk.Fetch(r.Context(), "https://login.microsoftonline.com/common/discovery/v2.0/keys")
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
if token.Method.Alg() != jwa.RS256.String() {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
kid, ok := token.Header["kid"].(string)
if !ok {
return nil, fmt.Errorf("kid header not found")
}
keys, ok := keySet.LookupKeyID(kid)
if !ok {
return nil, fmt.Errorf("key %v not found", kid)
}
publickey := &rsa.PublicKey{}
err = keys.Raw(publickey)
if err != nil {
return nil, fmt.Errorf("could not parse pubkey")
}
return publickey, nil
})
if err != nil {
return nil, err
}
return token, nil
}