-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmain.go
124 lines (100 loc) · 3.69 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
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
package main
import (
"errors"
"net/http"
"time"
"github.com/go-chi/chi/v5"
"github.com/go-chi/chi/v5/middleware"
"github.com/go-chi/cors"
"github.com/go-chi/oauth"
)
/*
Authorization Server Example
Generate Token using username & password
POST http://localhost:3000/token
User-Agent: Fiddler
Host: localhost:3000
Content-Length: 50
Content-Type: application/x-www-form-urlencoded
grant_type=password&username=user01&password=12345
Generate Token using clientID & secret
POST http://localhost:3000/auth
User-Agent: Fiddler
Host: localhost:3000
Content-Length: 66
Content-Type: application/x-www-form-urlencoded
grant_type=client_credentials&client_id=abcdef&client_secret=12345
RefreshTokenGrant Token
POST http://localhost:3000/token
User-Agent: Fiddler
Host: localhost:3000
Content-Length: 50
Content-Type: application/x-www-form-urlencoded
grant_type=refresh_token&refresh_token={the refresh_token obtained in the previous response}
*/
func main() {
r := chi.NewRouter()
r.Use(middleware.Logger)
r.Use(middleware.Recoverer)
r.Use(cors.Handler(cors.Options{
AllowedOrigins: []string{"*"},
AllowedMethods: []string{"GET", "PUT", "POST", "DELETE", "HEAD", "OPTION"},
AllowedHeaders: []string{"User-Agent", "Content-Type", "Accept", "Accept-Encoding", "Accept-Language", "Cache-Control", "Connection", "DNT", "Host", "Origin", "Pragma", "Referer"},
ExposedHeaders: []string{"Link"},
AllowCredentials: true,
MaxAge: 300, // Maximum value not ignored by any of major browsers
}))
registerAPI(r)
_ = http.ListenAndServe(":8080", r)
}
func registerAPI(r *chi.Mux) {
s := oauth.NewBearerServer(
"mySecretKey-10101",
time.Second*120,
&TestUserVerifier{},
nil)
r.Post("/token", s.UserCredentials)
r.Post("/auth", s.ClientCredentials)
}
// TestUserVerifier provides user credentials verifier for testing.
type TestUserVerifier struct {
}
// ValidateUser validates username and password returning an error if the user credentials are wrong
func (*TestUserVerifier) ValidateUser(username, password, scope string, r *http.Request) error {
if username == "user01" && password == "12345" {
return nil
}
return errors.New("wrong user")
}
// ValidateClient validates clientID and secret returning an error if the client credentials are wrong
func (*TestUserVerifier) ValidateClient(clientID, clientSecret, scope string, r *http.Request) error {
if clientID == "abcdef" && clientSecret == "12345" {
return nil
}
return errors.New("wrong client")
}
// ValidateCode validates token ID
func (*TestUserVerifier) ValidateCode(clientID, clientSecret, code, redirectURI string, r *http.Request) (string, error) {
return "", nil
}
// AddClaims provides additional claims to the token
func (*TestUserVerifier) AddClaims(tokenType oauth.TokenType, credential, tokenID, scope string, r *http.Request) (map[string]string, error) {
claims := make(map[string]string)
claims["customer_id"] = "1001"
claims["customer_data"] = `{"order_date":"2016-12-14","order_id":"9999"}`
return claims, nil
}
// AddProperties provides additional information to the token response
func (*TestUserVerifier) AddProperties(tokenType oauth.TokenType, credential, tokenID, scope string, r *http.Request) (map[string]string, error) {
props := make(map[string]string)
props["customer_name"] = "Gopher"
return props, nil
}
// ValidateTokenID validates token ID
func (*TestUserVerifier) ValidateTokenID(tokenType oauth.TokenType, credential, tokenID, refreshTokenID string) error {
return nil
}
// StoreTokenID saves the token id generated for the user
func (*TestUserVerifier) StoreTokenID(tokenType oauth.TokenType, credential, tokenID, refreshTokenID string) error {
return nil
}