This repository has been archived by the owner on Nov 25, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
143 lines (114 loc) · 4.22 KB
/
auth.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
/*************************************************************************
* MIT License
* Copyright (c) 2021 Model Rocket
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package api
import (
"context"
"net/http"
"reflect"
)
type (
// Authorizer is the api authorizer interface
Authorizer interface {
Authorize(r *http.Request, rt Route) (Principal, error)
CredentialType() CredentialType
}
// AuthorizerFunc performs an authorization and returns a context or error on failure
AuthorizerFunc func(r *http.Request, rt Route) (Principal, error)
// Claims is a basic claims interface
Claims interface {
// Set sets a value in the claims
Set(key string, value interface{})
// Get gets a value in the claims
Get(key string) interface{}
// All should return all of the claims as a map
All() map[string]interface{}
}
// Principal is an authorization principal
Principal interface {
// Type should return the principal type
Type() PrincipalType
// CredentialType should return the credential type, i.e. Basic or Bearer
CredentialType() CredentialType
// Credentials should return the raw principal credentials
Credentials() string
// Returns the principal's authorization claims
AuthClaims() Claims
}
// PrincipalType defines a principal type
PrincipalType string
// CredentialType is the credential type
CredentialType string
)
var (
contextKeyAuth = contextKey("api:auth")
prinType = reflect.TypeOf((*Principal)(nil)).Elem()
)
const (
// PrincipalTypeUser should be returned if the principal is a user
PrincipalTypeUser PrincipalType = "user"
// PrincipalTypeApplication should be used if the principal is an application
PrincipalTypeApplication PrincipalType = "application"
// CredentialTypeBasic is http 401 Basic auth
CredentialTypeBasic CredentialType = "Basic"
// CredentialTypeBearer is HTTP Bearer token authentication
CredentialTypeBearer CredentialType = "Bearer"
// CredentialTypeAPIKey is used for other api key based authentications
CredentialTypeAPIKey CredentialType = "ApiKey"
)
// Unauthorized fails auth and can used by routes in the authorizer chain as an
// explicit deny, this is useful when there are authorizers with optional components
func Unauthorized(r *http.Request) (interface{}, error) {
return nil, ErrUnauthorized
}
// AuthPrincipal the first auth principal that matches the target
func AuthPrincipal(ctx context.Context, target interface{}) bool {
prins, ok := ctx.Value(contextKeyAuth).([]Principal)
if prins == nil || !ok {
return false
}
val := reflect.ValueOf(target)
typ := val.Type()
if typ.Kind() != reflect.Ptr || val.IsNil() {
panic(ErrAuthUnacceptable)
}
if p := typ.Elem(); p.Kind() != reflect.Interface && !p.Implements(prinType) {
panic(ErrAuthUnacceptable)
}
targetType := typ.Elem()
for _, prin := range prins {
if reflect.TypeOf(prin).AssignableTo(targetType) {
val.Elem().Set(reflect.ValueOf(prin))
return true
}
}
return false
}
// RequirePrincipal panics with api.ErrUnauthorized on fail
func RequirePrincipal(ctx context.Context, target interface{}, t ...PrincipalType) {
if !AuthPrincipal(ctx, target) {
panic(ErrUnauthorized)
}
if len(t) > 0 && target.(Principal).Type() != t[0] {
panic(ErrUnauthorized)
}
}