/
authorize.go
146 lines (132 loc) · 4.53 KB
/
authorize.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package oauth2
import (
"fmt"
"github.com/cwkr/auth-server/internal/htmlutil"
"github.com/cwkr/auth-server/internal/httputil"
"github.com/cwkr/auth-server/internal/oauth2/clients"
"github.com/cwkr/auth-server/internal/people"
"github.com/cwkr/auth-server/internal/stringutil"
"log"
"net/http"
"net/url"
"regexp"
"strings"
)
func IntersectScope(availableScope, requestedScope string) string {
var results []string
var as, rs = strings.Fields(availableScope), strings.Fields(requestedScope)
for _, aw := range as {
for _, rw := range rs {
if strings.EqualFold(aw, rw) {
results = append(results, aw)
}
}
}
return strings.Join(results, " ")
}
type authorizeHandler struct {
basePath string
tokenService TokenCreator
peopleStore people.Store
clientStore clients.Store
scope string
sessionName string
}
func (a *authorizeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s", r.Method, r.URL)
var (
timing = httputil.NewTiming()
responseType = strings.ToLower(strings.TrimSpace(r.FormValue("response_type")))
clientID = strings.ToLower(strings.TrimSpace(r.FormValue("client_id")))
redirectURI = strings.TrimSpace(r.FormValue("redirect_uri"))
state = strings.TrimSpace(r.FormValue("state"))
scope = strings.TrimSpace(r.FormValue("scope"))
challenge = strings.TrimSpace(r.FormValue("code_challenge"))
challengeMethod = strings.TrimSpace(r.FormValue("code_challenge_method"))
nonce = strings.TrimSpace(r.FormValue("nonce"))
sessionName = a.sessionName
user User
)
if stringutil.IsAnyEmpty(responseType, clientID, redirectURI) {
htmlutil.Error(w, a.basePath, "client_id, redirect_uri and response_type parameters are required", http.StatusBadRequest)
return
}
var client clients.Client
if c, err := a.clientStore.Lookup(clientID); err != nil {
Error(w, ErrorUnauthorizedClient, ErrorInvalidClient, http.StatusForbidden)
return
} else {
if responseType == ResponseTypeToken && client.DisableImplicit {
htmlutil.Error(w, a.basePath, ErrorUnsupportedGrantType, http.StatusBadRequest)
return
}
client = *c
}
if client.SessionName != "" {
sessionName = client.SessionName
}
if client.RedirectURIPattern != "" && !regexp.MustCompile(client.RedirectURIPattern).MatchString(redirectURI) {
htmlutil.Error(w, a.basePath, ErrorRedirectURIMismatch, http.StatusBadRequest)
return
}
if uid, active := a.peopleStore.IsSessionActive(r, sessionName); active {
timing.Start("store")
if person, err := a.peopleStore.Lookup(uid); err == nil {
user = User{UserID: uid, Person: *person}
} else {
htmlutil.Error(w, a.basePath, err.Error(), http.StatusInternalServerError)
return
}
timing.Stop("store")
} else {
httputil.RedirectQuery(w, r, strings.TrimRight(a.tokenService.Issuer(), "/")+"/login", r.URL.Query())
return
}
switch responseType {
case ResponseTypeToken:
timing.Start("jwtgen")
var x, err = a.tokenService.GenerateAccessToken(user, user.UserID, clientID, IntersectScope(a.scope, scope))
if err != nil {
htmlutil.Error(w, a.basePath, err.Error(), http.StatusInternalServerError)
return
}
timing.Stop("jwtgen")
httputil.NoCache(w)
timing.Report(w)
httputil.RedirectFragment(w, r, redirectURI, url.Values{
"access_token": {x},
"token_type": {"Bearer"},
"expires_in": {fmt.Sprint(a.tokenService.AccessTokenTTL())},
"state": {state},
})
case ResponseTypeCode:
if challengeMethod != "" {
if challenge == "" || challengeMethod != "S256" {
htmlutil.Error(w, a.basePath, "code_challenge and code_challenge_method=S256 required for PKCE", http.StatusInternalServerError)
return
}
}
timing.Start("jwtgen")
var x, err = a.tokenService.GenerateAuthCode(user.UserID, clientID, IntersectScope(a.scope, scope), challenge, nonce)
if err != nil {
htmlutil.Error(w, a.basePath, err.Error(), http.StatusInternalServerError)
return
}
timing.Stop("jwtgen")
httputil.NoCache(w)
timing.Report(w)
httputil.RedirectQuery(w, r, redirectURI, url.Values{"code": {x}, "state": {state}})
default:
htmlutil.Error(w, a.basePath, ErrorUnsupportedGrantType, http.StatusBadRequest)
}
}
func AuthorizeHandler(basePath string, tokenService TokenCreator, peopleStore people.Store, clientStore clients.Store, scope, sessionName string) http.Handler {
return &authorizeHandler{
basePath: basePath,
tokenService: tokenService,
peopleStore: peopleStore,
clientStore: clientStore,
scope: scope,
sessionName: sessionName,
}
}