-
Notifications
You must be signed in to change notification settings - Fork 21
/
authorizer.go
78 lines (67 loc) · 2.52 KB
/
authorizer.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
// Copyright © 2019 by PACE Telematics GmbH. All rights reserved.
// Created at 2019/10/30 by Charlotte Pröller
package oauth2
import (
"context"
"fmt"
"net/http"
"github.com/pace/bricks/http/security"
)
// Authorizer is an implementation of security.Authorizer for OAuth2
// it uses introspection to get user data and can check the scope
type Authorizer struct {
introspection TokenIntrospecter
scope Scope
config *Config
}
// Flow is a part of the OAuth2 config from the security schema
type Flow struct {
AuthorizationURL string
TokenURL string
RefreshURL string
Scopes map[string]string
}
// Config contains the configuration from the api definition - currently not used
type Config struct {
Description string
Implicit *Flow
Password *Flow
ClientCredentials *Flow
AuthorizationCode *Flow
}
// NewAuthorizer creates an Authorizer for a specific TokenIntrospecter
// This Authorizer does not check the scope
func NewAuthorizer(introspector TokenIntrospecter, cfg *Config) *Authorizer {
return &Authorizer{introspection: introspector, config: cfg}
}
// WithScope returns a new Authorizer with the same TokenIntrospecter and the same Config that also checks the scope of a request
func (a *Authorizer) WithScope(tok string) *Authorizer {
return &Authorizer{introspection: a.introspection, config: a.config, scope: Scope(tok)}
}
// Authorize authorizes a request with an introspection and validates the scope
// Success: returns context with the introspection result and true
// Error: writes all errors directly to response, returns unchanged context and false
func (a *Authorizer) Authorize(r *http.Request, w http.ResponseWriter) (context.Context, bool) {
ctx, ok := introspectRequest(r, w, a.introspection)
// Check if introspection was successful
if !ok {
return ctx, ok
}
// If the Authorizer has no scope, the request is valid, otherwise the scope must be validated
if a.scope != "" {
// Check if the scope is valid for this user
ok = validateScope(ctx, w, a.scope)
}
return ctx, ok
}
func validateScope(ctx context.Context, w http.ResponseWriter, req Scope) bool {
if !HasScope(ctx, req) {
http.Error(w, fmt.Sprintf("Forbidden - requires scope %q", req), http.StatusForbidden)
return false
}
return true
}
// CanAuthorizeRequest returns true, if the request contains a token in the configured header, otherwise false
func (a *Authorizer) CanAuthorizeRequest(r *http.Request) bool {
return security.GetBearerTokenFromHeader(r.Header.Get(oAuth2Header)) != ""
}