forked from openshift/osin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
195 lines (181 loc) · 6.11 KB
/
util.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package osin
import (
"encoding/base64"
"github.com/stretchr/objx"
"net/http"
"strings"
)
// BasicAuth defines the values required for basic authentication.
type BasicAuth struct {
Username string
Password string
}
// CheckBasicAuth reads Basic authorization from the Authorization
// header.
func CheckBasicAuth(r *http.Request) (*BasicAuth, *HttpError) {
if r.Header.Get("Authorization") == "" {
return nil, nil
}
s := strings.SplitN(r.Header.Get("Authorization"), " ", 2)
if len(s) != 2 || s[0] != "Basic" {
return nil, &HttpError{
Status: http.StatusBadRequest,
Message: "Invalid authorization header",
}
}
b, err := base64.StdEncoding.DecodeString(s[1])
if err != nil {
return nil, &HttpError{
Status: http.StatusBadRequest,
Message: "Could not decode basic auth: " + err.Error(),
}
}
pair := strings.SplitN(string(b), ":", 2)
if len(pair) != 2 {
return nil, &HttpError{
Status: http.StatusBadRequest,
Message: "Basic authorization must be a base64-encoded id:secret pair",
}
}
return &BasicAuth{Username: pair[0], Password: pair[1]}, nil
}
// ChecClientAuth checks for client_id and client_secret in the
// Authorization header and (if useparams is true) request parameters.
func CheckClientAuth(r *http.Request, params objx.Map, useparams bool) (*BasicAuth, *HttpError) {
if useparams {
ret := &BasicAuth{
Username: params.Get("client_id").Str(),
Password: params.Get("client_secret").Str(),
}
if ret.Username != "" && ret.Password != "" {
return ret, nil
}
}
return CheckBasicAuth(r)
}
// GetValidAuth loads authorization using CheckClientAuth, then
// ensures that the authorization is valid. It returns a *HttpError
// if there are any problems with the request.
func GetValidAuth(request *http.Request, params objx.Map, allowSecretInParams bool) (*BasicAuth, *HttpError) {
auth, err := CheckClientAuth(request, params, allowSecretInParams)
if err != nil || auth == nil {
if err == nil {
err = &HttpError{
Status: http.StatusUnauthorized,
Message: "No client credentials received",
}
}
return nil, err
}
return auth, nil
}
// GetValidClient takes a client id and a *Response, then
// tries to load a client from storage and validate that client. It
// will return nil for the returned Client and a *HttpError if there
// are any problems locating or validating the requested client (i.e.
// if the client doesn't exist or has an empty GetRedirectUri()
// response), or the validated Client and nil for an error otherwise.
func (s *Server) GetValidClient(id string) (Client, *HttpError) {
client, err := s.Storage.GetClient(id)
if err != nil {
if httpErr, ok := err.(*HttpError); ok {
return nil, httpErr
}
return nil, &HttpError{
Status: http.StatusInternalServerError,
Message: err.Error(),
}
}
if client == nil || client.GetRedirectUri() == "" {
return nil, deferror.Get(E_UNAUTHORIZED_CLIENT)
}
return client, nil
}
// GetValidClientWithSecret takes a client id, secret, and a
// *Response, then returns the client if both GetValidClient returns a
// valid client and the passed in secret matches the client's secret.
func (s *Server) GetValidClientWithSecret(id, secret string) (Client, *HttpError) {
client, err := s.GetValidClient(id)
if err != nil {
return nil, err
}
if client.GetSecret() != secret {
return nil, deferror.Get(E_UNAUTHORIZED_CLIENT)
}
return client, nil
}
// GetValidAuthData takes an authorization code and a *Response, then
// tries to load an AuthorizeData from storage and validate that data.
// It will return nil for the returned AuthorizeData and an error if
// there are any problems locating or validating the requested data
// (i.e. if the AuthorizeData's Client value returned from GetClient()
// is nil or has an empty GetRedirectUri() response), or the validated
// AuthorizeData and nil for an error otherwise.
func (s *Server) GetValidAuthData(code string) (AuthorizeData, *HttpError) {
authData, err := s.Storage.LoadAuthorize(code)
if err != nil {
if httpErr, ok := err.(*HttpError); ok {
return nil, httpErr
}
return nil, &HttpError{
Status: http.StatusInternalServerError,
Message: err.Error(),
}
}
if authData.GetClient() == nil || authData.GetClient().GetRedirectUri() == "" {
return nil, deferror.Get(E_UNAUTHORIZED_CLIENT)
}
if authData.IsExpired() {
return nil, deferror.Get(E_INVALID_GRANT)
}
return authData, nil
}
// GetValidAccessData takes a access token and a *Response, then
// tries to load an AccessData from storage and validate that data.
// It will return nil for the returned AccessData and an error if
// there are any problems locating or validating the requested data
// (i.e. if the AccessData's Client value returned from GetClient()
// is nil or has an empty GetRedirectUri() response), or the validated
// AccessData and nil for an error otherwise.
func (s *Server) GetValidAccessData(token string) (AccessData, *HttpError) {
access, err := s.Storage.LoadAccess(token)
if err != nil {
if httpErr, ok := err.(*HttpError); ok {
return nil, httpErr
}
return nil, &HttpError{
Status: http.StatusInternalServerError,
Message: err.Error(),
}
}
if access.GetClient() == nil {
return nil, deferror.Get(E_UNAUTHORIZED_CLIENT)
}
if access.IsExpired() {
return nil, deferror.Get(E_INVALID_GRANT)
}
return access, nil
}
// GetValidRefresh takes a refresh token and a *Response, then
// tries to load an AccessData from storage and validate that data.
// It will return nil for the returned AccessData and an error if
// there are any problems locating or validating the requested data
// (i.e. if the AccessData's Client value returned from GetClient()
// is nil or has an empty GetRedirectUri() response), or the validated
// AccessData and nil for an error otherwise.
func (s *Server) GetValidRefresh(token string) (AccessData, *HttpError) {
access, err := s.Storage.LoadRefresh(token)
if err != nil {
if httpErr, ok := err.(*HttpError); ok {
return nil, httpErr
}
return nil, &HttpError{
Status: http.StatusInternalServerError,
Message: err.Error(),
}
}
if access.GetClient() == nil {
return nil, deferror.Get(E_UNAUTHORIZED_CLIENT)
}
return access, nil
}