-
Notifications
You must be signed in to change notification settings - Fork 21
/
oauth2.go
199 lines (174 loc) · 6.07 KB
/
oauth2.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
196
197
198
199
// Copyright © 2018 by PACE Telematics GmbH. All rights reserved.
// Created at 2018/09/03 by Mohamed Wael Khobalatte
// Package oauth2 provides a middelware that introspects the auth token on
// behalf of PACE services and populate the request context with useful information
// when the token is valid, otherwise aborts the request.
package oauth2
import (
"context"
"errors"
"net/http"
opentracing "github.com/opentracing/opentracing-go"
olog "github.com/opentracing/opentracing-go/log"
"github.com/pace/bricks/http/security"
"github.com/pace/bricks/maintenance/log"
)
// Deprecated: Middleware holds data necessary for Oauth processing - Deprecated for generated apis,
// use the generated Authentication Backend of the API with oauth2.Authorizer
type Middleware struct {
Backend TokenIntrospecter
}
// Deprecated: NewMiddleware creates a new Oauth middleware - Deprecated for generated apis,
// use the generated AuthenticationBackend of the API with oauth2.Authorizer
func NewMiddleware(backend TokenIntrospecter) *Middleware {
return &Middleware{Backend: backend}
}
// Handler will parse the bearer token, introspect it, and put the token and other
// relevant information back in the context.
func (m *Middleware) Handler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx, isOk := introspectRequest(r, w, m.Backend)
if !isOk {
return
}
next.ServeHTTP(w, r.WithContext(ctx))
})
}
type token struct {
value string
userID string
clientID string
scope Scope
backend interface{}
}
const oAuth2Header = "Authorization"
// GetValue returns the oauth2 token of the current user
func (t *token) GetValue() string {
return t.value
}
// IntrospectRequest introspects the requests and handles all errors:
// Success: it returns a context containing the introspection result and true
// if the introspection was successful
// Error: The function writes the error in the Response and creates a log-message
// with more details and returns nil and false if any error occurs during the introspection
func introspectRequest(r *http.Request, w http.ResponseWriter, tokenIntro TokenIntrospecter) (context.Context, bool) {
// Setup tracing
span, ctx := opentracing.StartSpanFromContext(r.Context(), "Oauth2")
defer span.Finish()
tok := security.GetBearerTokenFromHeader(r.Header.Get(oAuth2Header))
if tok == "" {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return nil, false
}
s, err := tokenIntro.IntrospectToken(ctx, tok)
if err != nil {
switch {
case errors.Is(err, ErrBadUpstreamResponse):
fallthrough
case errors.Is(err, ErrUpstreamConnection):
http.Error(w, err.Error(), http.StatusBadGateway)
case errors.Is(err, ErrInvalidToken):
http.Error(w, err.Error(), http.StatusUnauthorized)
default:
http.Error(w, err.Error(), http.StatusInternalServerError)
}
log.Req(r).Info().Msg(err.Error())
return nil, false
}
t := fromIntrospectResponse(s, tok)
ctx = security.ContextWithToken(ctx, &t)
log.Req(r).Info().
Str("client_id", t.clientID).
Str("user_id", t.userID).
Msg("Oauth2")
span.LogFields(olog.String("client_id", t.clientID), olog.String("user_id", t.userID))
return ctx, true
}
func fromIntrospectResponse(s *IntrospectResponse, tokenValue string) token {
t := token{
userID: s.UserID,
value: tokenValue,
clientID: s.ClientID,
backend: s.Backend,
}
t.scope = Scope(s.Scope)
return t
}
// Request adds Authorization token to r
func Request(r *http.Request) *http.Request {
tok, ok := security.GetTokenFromContext(r.Context())
if ok {
r.Header.Set("Authorization", security.GetAuthHeader(tok))
}
return r
}
// HasScope extracts an access token from context and checks if
// the permissions represented by the provided scope are included in the valid scope.
func HasScope(ctx context.Context, scope Scope) bool {
tok, _ := security.GetTokenFromContext(ctx)
oauth2token, ok := tok.(*token)
if !ok {
return false
}
return scope.IsIncludedIn(oauth2token.scope)
}
// UserID returns the userID stored in ctx
func UserID(ctx context.Context) (string, bool) {
tok, _ := security.GetTokenFromContext(ctx)
oauth2token, ok := tok.(*token)
if !ok {
return "", false
}
return oauth2token.userID, true
}
// Scopes returns the scopes stored in ctx
func Scopes(ctx context.Context) []string {
tok, _ := security.GetTokenFromContext(ctx)
oauth2token, ok := tok.(*token)
if !ok {
return []string{}
}
return oauth2token.scope.toSlice()
}
// ClientID returns the clientID stored in ctx
func ClientID(ctx context.Context) (string, bool) {
tok, _ := security.GetTokenFromContext(ctx)
oauth2token, ok := tok.(*token)
if !ok {
return "", false
}
return oauth2token.clientID, true
}
// Backend returns the backend stored in the context. It identifies the
// authorization backend for the token.
func Backend(ctx context.Context) (interface{}, bool) {
tok, _ := security.GetTokenFromContext(ctx)
oauth2token, ok := tok.(*token)
if !ok {
return nil, false
}
return oauth2token.backend, true
}
// ContextTransfer sources the oauth2 token from the sourceCtx
// and returning a new context based on the targetCtx
func ContextTransfer(sourceCtx context.Context, targetCtx context.Context) context.Context {
tok, _ := security.GetTokenFromContext(sourceCtx)
return security.ContextWithToken(targetCtx, tok)
}
// Deprecated: BearerToken was moved to the security package,
// because it's used by apiKey and oauth2 authorization.
// BearerToken returns the bearer token stored in ctx
func BearerToken(ctx context.Context) (string, bool) {
if tok, ok := security.GetTokenFromContext(ctx); ok {
return tok.GetValue(), true
}
return "", false
}
// Deprecated: WithBearerToken was moved to the security package,
// because it's used by api key and oauth2 authorization.
// returns a new context with the given bearer token
// Use security.BearerToken() to retrieve the token. Use Request() to obtain a request
// with the Authorization header set accordingly.
func WithBearerToken(ctx context.Context, bearerToken string) context.Context {
return security.ContextWithToken(ctx, &token{value: bearerToken})
}