-
Notifications
You must be signed in to change notification settings - Fork 1
/
authn.go
132 lines (109 loc) · 2.81 KB
/
authn.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
package main
import (
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strings"
"sync"
"time"
"github.com/julienschmidt/httprouter"
"github.com/square/go-jose/v3/jwt"
)
var (
lookup = sync.Map{}
)
type Claims struct {
Ac string `json:"ac,omitempty"`
Aud string `json:"aud,omitempty"`
}
type Ac []struct {
Scope string `json:"Scope,omitempty"`
}
func extractParams(bearer string) (httprouter.Params, error) {
token, err := jwt.ParseSigned(bearer)
if err != nil {
return nil, err
}
var claims Claims
if err := token.UnsafeClaimsWithoutVerification(&claims); err != nil {
return nil, err
}
var ac Ac
if err := json.Unmarshal([]byte(claims.Ac), &ac); err != nil {
return nil, err
}
params := httprouter.Params{
httprouter.Param{
Key: "audience",
Value: claims.Aud,
},
httprouter.Param{
Key: "scope",
Value: ac[0].Scope,
},
}
return params, nil
}
func newRequest(baseURL string, authorization string) (*http.Request, error) {
parsedURL, err := url.ParseRequestURI(baseURL + "_apis/artifactcache/cache?keys=e3b0c44298fc1c149afbf4c8996fb924&version=27ae41e4649b934ca495991b7852b855")
if err != nil {
return nil, err
}
request := http.Request{
Method: http.MethodGet,
URL: parsedURL,
Header: http.Header{
"Accept": []string{
"application/json;api-version=6.0-preview.1",
},
"Authorization": []string{
authorization,
},
},
}
return &request, nil
}
func validateToken(baseURL string, authorization string) error {
req, err := newRequest(baseURL, authorization)
if err != nil {
return err
}
res, err := http.DefaultClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if statusCode := res.StatusCode; statusCode != http.StatusOK && statusCode != http.StatusNoContent {
return errors.New(http.StatusText(statusCode))
}
return nil
}
func authenticated(h httprouter.Handle) httprouter.Handle {
return func(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
actionsCacheURL, err := base64.RawURLEncoding.DecodeString(ps.ByName("actionsCacheURL"))
if err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
authorization := r.Header.Get("Authorization")
bearer := strings.SplitAfter(authorization, " ")[1]
signature := strings.SplitAfter(authorization, ".")[2]
key := fmt.Sprintf("%s#%s", actionsCacheURL, signature)
if t, ok := lookup.Load(key); !ok || t.(time.Time).Before(time.Now().Add(-5*time.Minute)) {
if err := validateToken(string(actionsCacheURL), authorization); err != nil {
http.Error(w, err.Error(), http.StatusUnauthorized)
return
}
lookup.Store(key, time.Now())
}
params, err := extractParams(bearer)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
h(w, r, append(ps, params...))
}
}