forked from openfaas/go-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexchange.go
57 lines (46 loc) · 1.34 KB
/
exchange.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
package sdk
import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/url"
"strings"
)
// Exchange an OIDC ID Token from an IdP for OpenFaaS token
// using the token exchange grant type.
// tokenURL should be the OpenFaaS token endpoint within the internal OIDC service
func ExchangeIDToken(tokenURL, rawIDToken string) (*Token, error) {
v := url.Values{}
v.Set("grant_type", "urn:ietf:params:oauth:grant-type:token-exchange")
v.Set("subject_token_type", "urn:ietf:params:oauth:token-type:id_token")
v.Set("subject_token", rawIDToken)
u, _ := url.Parse(tokenURL)
req, err := http.NewRequest(http.MethodPost, u.String(), strings.NewReader(v.Encode()))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
res, err := http.DefaultClient.Do(req)
if err != nil {
return nil, err
}
if res.Body != nil {
defer res.Body.Close()
}
body, err := io.ReadAll(res.Body)
if err != nil {
return nil, fmt.Errorf("cannot fetch token: %v", err)
}
if code := res.StatusCode; code < 200 || code > 299 {
return nil, fmt.Errorf("cannot fetch token: %v\nResponse: %s", res.Status, body)
}
tj := &tokenJSON{}
if err := json.Unmarshal(body, tj); err != nil {
return nil, fmt.Errorf("unable to unmarshal token: %s", err)
}
return &Token{
IDToken: tj.AccessToken,
Expiry: tj.expiry(),
}, nil
}