-
Notifications
You must be signed in to change notification settings - Fork 1
/
remote_supplier.go
93 lines (84 loc) · 2.48 KB
/
remote_supplier.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
/*
* Copyright 2022 Armory, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package token
import (
"context"
"encoding/json"
"fmt"
"github.com/lestrrat-go/jwx/jwt"
"io/ioutil"
"net/http"
"net/url"
"strings"
"time"
)
func NewRemoteTokenSupplier(cloud ArmoryCloud) *RemoteTokenSupplier {
return &RemoteTokenSupplier{
settings: cloud,
}
}
type RemoteTokenSupplier struct {
settings ArmoryCloud
}
func (r *RemoteTokenSupplier) GetToken(ctx context.Context) (string, *time.Time, error) {
data := url.Values{}
data.Set("grant_type", "client_credentials")
data.Set("client_id", r.settings.ClientId)
data.Set("client_secret", r.settings.Secret)
data.Set("audience", r.settings.Audience)
req, err := http.NewRequest(http.MethodPost, r.settings.TokenIssuerUrl, strings.NewReader(data.Encode()))
if err != nil {
return "", nil, err
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Add("accept", "application/json")
c := &http.Client{}
res, err := c.Do(req)
if err != nil {
return "", nil, err
}
if res.StatusCode < 200 || res.StatusCode > 299 {
return "", nil, fmt.Errorf("unexpected status code while getting token %d", res.StatusCode)
}
defer res.Body.Close()
tk, err := ioutil.ReadAll(res.Body)
if err != nil {
return "", nil, err
}
rt := &remoteToken{}
if err := json.Unmarshal(tk, rt); err != nil {
return "", nil, fmt.Errorf("unable to parse response from %s: %w", r.settings.TokenIssuerUrl, err)
}
if rt.AccessToken == "" {
return "", nil, fmt.Errorf("no access_token returned from %s", r.settings.TokenIssuerUrl)
}
t, err := jwt.Parse([]byte(rt.AccessToken), r.parseOptions()...)
if err != nil {
return "", nil, err
}
exp := t.Expiration()
return rt.AccessToken, &exp, nil
}
func (r *RemoteTokenSupplier) parseOptions() []jwt.ParseOption {
var opts []jwt.ParseOption
if r.settings.Verify {
opts = append(opts, jwt.WithValidate(true))
}
return opts
}
type remoteToken struct {
AccessToken string `json:"access_token"`
}