Skip to content

Commit 3621840

Browse files
committed
Updated
1 parent 3ef29f4 commit 3621840

File tree

3 files changed

+596
-3
lines changed

3 files changed

+596
-3
lines changed

.editorconfig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
[*.go]
2-
end_of_line = cr
2+
end_of_line = lf

server/handler.go

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,64 @@
1-
package serverimport ( "net/http" "time" "github.com/go-oauth2/oauth2/v4" "github.com/go-oauth2/oauth2/v4/errors")type ( // ClientInfoHandler get client info from request ClientInfoHandler func(r *http.Request) (clientID, clientSecret string, err error) // ClientAuthorizedHandler check the client allows to use this authorization grant type ClientAuthorizedHandler func(clientID string, grant oauth2.GrantType) (allowed bool, err error) // ClientScopeHandler check the client allows to use scope ClientScopeHandler func(tgr *oauth2.TokenGenerateRequest) (allowed bool, err error) // UserAuthorizationHandler get user id from request authorization UserAuthorizationHandler func(w http.ResponseWriter, r *http.Request) (userID string, err error) // PasswordAuthorizationHandler get user id from username and password PasswordAuthorizationHandler func(username, password string) (userID string, err error) // RefreshingScopeHandler check the scope of the refreshing token RefreshingScopeHandler func(tgr *oauth2.TokenGenerateRequest, oldScope string) (allowed bool, err error) // ResponseErrorHandler response error handing ResponseErrorHandler func(re *errors.Response) // InternalErrorHandler internal error handing InternalErrorHandler func(err error) (re *errors.Response) // AuthorizeScopeHandler set the authorized scope AuthorizeScopeHandler func(w http.ResponseWriter, r *http.Request) (scope string, err error) // AccessTokenExpHandler set expiration date for the access token AccessTokenExpHandler func(w http.ResponseWriter, r *http.Request) (exp time.Duration, err error) // ExtensionFieldsHandler in response to the access token with the extension of the field ExtensionFieldsHandler func(ti oauth2.TokenInfo) (fieldsValue map[string]interface{}))// ClientFormHandler get client data from formfunc ClientFormHandler(r *http.Request) (string, string, error) { clientID := r.Form.Get("client_id") if clientID == "" { return "", "", errors.ErrInvalidClient } clientSecret := r.Form.Get("client_secret") return clientID, clientSecret, nil}// ClientBasicHandler get client data from basic authorizationfunc ClientBasicHandler(r *http.Request) (string, string, error) { username, password, ok := r.BasicAuth() if !ok { return "", "", errors.ErrInvalidClient } return username, password, nil}
1+
package server
2+
3+
import (
4+
"net/http"
5+
"time"
6+
7+
"github.com/go-oauth2/oauth2/v4"
8+
"github.com/go-oauth2/oauth2/v4/errors"
9+
)
10+
11+
type (
12+
// ClientInfoHandler get client info from request
13+
ClientInfoHandler func(r *http.Request) (clientID, clientSecret string, err error)
14+
15+
// ClientAuthorizedHandler check the client allows to use this authorization grant type
16+
ClientAuthorizedHandler func(clientID string, grant oauth2.GrantType) (allowed bool, err error)
17+
18+
// ClientScopeHandler check the client allows to use scope
19+
ClientScopeHandler func(tgr *oauth2.TokenGenerateRequest) (allowed bool, err error)
20+
21+
// UserAuthorizationHandler get user id from request authorization
22+
UserAuthorizationHandler func(w http.ResponseWriter, r *http.Request) (userID string, err error)
23+
24+
// PasswordAuthorizationHandler get user id from username and password
25+
PasswordAuthorizationHandler func(username, password string) (userID string, err error)
26+
27+
// RefreshingScopeHandler check the scope of the refreshing token
28+
RefreshingScopeHandler func(tgr *oauth2.TokenGenerateRequest, oldScope string) (allowed bool, err error)
29+
30+
// ResponseErrorHandler response error handing
31+
ResponseErrorHandler func(re *errors.Response)
32+
33+
// InternalErrorHandler internal error handing
34+
InternalErrorHandler func(err error) (re *errors.Response)
35+
36+
// AuthorizeScopeHandler set the authorized scope
37+
AuthorizeScopeHandler func(w http.ResponseWriter, r *http.Request) (scope string, err error)
38+
39+
// AccessTokenExpHandler set expiration date for the access token
40+
AccessTokenExpHandler func(w http.ResponseWriter, r *http.Request) (exp time.Duration, err error)
41+
42+
// ExtensionFieldsHandler in response to the access token with the extension of the field
43+
ExtensionFieldsHandler func(ti oauth2.TokenInfo) (fieldsValue map[string]interface{})
44+
)
45+
46+
47+
// ClientFormHandler get client data from form
48+
func ClientFormHandler(r *http.Request) (string, string, error) {
49+
clientID := r.Form.Get("client_id")
50+
if clientID == "" {
51+
return "", "", errors.ErrInvalidClient
52+
}
53+
clientSecret := r.Form.Get("client_secret")
54+
return clientID, clientSecret, nil
55+
}
56+
57+
// ClientBasicHandler get client data from basic authorization
58+
func ClientBasicHandler(r *http.Request) (string, string, error) {
59+
username, password, ok := r.BasicAuth()
60+
if !ok {
61+
return "", "", errors.ErrInvalidClient
62+
}
63+
return username, password, nil
64+
}

0 commit comments

Comments
 (0)