/
revoke.go
86 lines (74 loc) · 2.55 KB
/
revoke.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
package oauth2
import (
"github.com/cwkr/auth-server/internal/httputil"
"github.com/cwkr/auth-server/internal/oauth2/clients"
"github.com/cwkr/auth-server/internal/oauth2/trl"
"github.com/cwkr/auth-server/internal/stringutil"
"log"
"net/http"
"strings"
"unicode/utf8"
)
type revokeHandler struct {
tokenCreator TokenCreator
trlStore trl.Store
clientStore clients.Store
}
func (j *revokeHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("%s %s", r.Method, r.URL)
httputil.AllowCORS(w, r, []string{http.MethodPost, http.MethodOptions}, false)
if r.Method == http.MethodOptions {
w.WriteHeader(http.StatusNoContent)
return
}
var (
//timing = httputil.NewTiming()
clientID, clientSecret, basicAuth = r.BasicAuth()
token = strings.TrimSpace(r.PostFormValue("token"))
tokenTypeHint = strings.TrimSpace(r.PostFormValue("token_type_hint"))
)
// when not using basic auth load client_id and client_secret parameters
if !basicAuth {
clientID = strings.TrimSpace(r.PostFormValue("client_id"))
clientSecret = r.PostFormValue("client_secret")
}
// debug output of parameters
log.Printf("client_id=%q client_secret=%q token_type_hint=%q token=%q",
clientID, strings.Repeat("*", utf8.RuneCountInString(clientSecret)), tokenTypeHint, token)
if clientSecret != "" {
if _, err := j.clientStore.Authenticate(clientID, clientSecret); err != nil {
Error(w, ErrorUnauthorizedClient, err.Error(), http.StatusUnauthorized)
return
}
} else {
if _, err := j.clientStore.Lookup(clientID); err != nil {
Error(w, ErrorUnauthorizedClient, err.Error(), http.StatusUnauthorized)
return
}
}
if stringutil.IsAnyEmpty(clientID, token) {
Error(w, ErrorInvalidRequest, "client_id and token parameters are required", http.StatusBadRequest)
return
}
if claims, err := j.tokenCreator.Verify(token, ""); err == nil {
if claims.TokenID == "" {
Error(w, ErrorInvalidRequest, "token without id (jti)", http.StatusInternalServerError)
return
}
if err := j.trlStore.Put(claims.TokenID, claims.Expiry.Time()); err != nil {
log.Printf("!!! %s", err)
Error(w, ErrorInternal, err.Error(), http.StatusInternalServerError)
return
}
} else {
log.Printf("!!! Token already invalid: %s", err)
}
w.WriteHeader(http.StatusNoContent)
}
func RevokeHandler(tokenCreator TokenCreator, clientStore clients.Store, trlStore trl.Store) http.Handler {
return &revokeHandler{
tokenCreator: tokenCreator,
clientStore: clientStore,
trlStore: trlStore,
}
}