-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathohauth.go
66 lines (58 loc) · 1.77 KB
/
ohauth.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
package ohauth
import (
"net/http"
"time"
"github.com/dgrijalva/jwt-go"
)
var issuer = &defaultIssuer{}
var tokenizer = NewJWTTokenizer(jwt.SigningMethodRS256)
// Provider configures an OAuth 2.0 provider that can authorize clients by
// issuing signed access tokens
type Provider struct {
// Authorization and Authentication endpoints
URL *StrictURL
// Authenticator is used for to parse sessions and authenticate via password grants
Authenticator Authenticator
// Data store for clients and tokens
Store Store
// Tokenizer is required to generate code, id, access and refresh tokens
Tokenizer Tokenizer
// Issuer is used to determine claim values when issuing tokens
Issuer Issuer
}
// NewProvider creates a provider configured with the default tokenizer and
// issuer.
func NewProvider(u *StrictURL, authn Authenticator, store Store) *Provider {
return &Provider{
u,
authn,
store,
tokenizer,
issuer,
}
}
// Handler returns an http.Handler that can be integrated into web applications
func (p *Provider) Handler() http.Handler {
mux := http.NewServeMux()
mux.HandleFunc(p.URL.Path+"/authorize", func(w http.ResponseWriter, r *http.Request) {
defer func() { _ = r.Body.Close() }()
if err := handleAuthorize(&context{p, w, r, time.Now()}); err != nil {
panic(err)
}
})
mux.HandleFunc(p.URL.Path+"/token", func(w http.ResponseWriter, r *http.Request) {
defer func() { _ = r.Body.Close() }()
if err := handleGrant(&context{p, w, r, time.Now()}); err != nil {
panic(err)
}
})
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == "GET" || r.Method == "POST" {
mux.ServeHTTP(w, r)
}
w.WriteHeader(http.StatusMethodNotAllowed)
if _, err := w.Write([]byte("Method not allowed")); err != nil {
panic(err)
}
})
}