-
Notifications
You must be signed in to change notification settings - Fork 68
/
jwt.go
152 lines (125 loc) · 4.18 KB
/
jwt.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
/*
Copyright 2022 TriggerMesh 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 auth
import (
"crypto/rsa"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
"strings"
"time"
"github.com/golang-jwt/jwt/v4"
"go.uber.org/zap"
)
const (
// grant type for OAuth JWT.
// See: https://tools.ietf.org/html/rfc7523#page-10
grantJWT = "urn:ietf:params:oauth:grant-type:jwt-bearer"
oauthTokenPath = "/services/oauth2/token"
)
// JWTAuthenticator is the JWT OAuth implementation.
// See: https://help.salesforce.com/articleView?id=remoteaccess_oauth_jwt_flow.htm
type JWTAuthenticator struct {
authURL string
signKey *rsa.PrivateKey
claims *claims
client *http.Client
logger *zap.SugaredLogger
}
type claims struct {
jwt.RegisteredClaims
}
// Credentials returned from Salesforce Auth.
type Credentials struct {
Token string `json:"access_token"`
TokenType string `json:"token_type"`
RefreshToken string `json:"refresh_token"`
InstanceURL string `json:"instance_url"`
ID string `json:"id"`
Signature string `json:"signature"`
Scope string `json:"scope"`
IDToken string `json:"id_token"`
CommunityURL string `json:"sfdc_community_url"`
CommunityID string `json:"sfdc_community_id"`
}
// NewJWTAuthenticator creates an OAuth JWT authenticator for Salesforce.
func NewJWTAuthenticator(certKey, clientID, user, server string, client *http.Client, logger *zap.SugaredLogger) (*JWTAuthenticator, error) {
audience := strings.TrimSuffix(server, "/")
signKey, err := jwt.ParseRSAPrivateKeyFromPEM([]byte(certKey))
if err != nil {
return nil, fmt.Errorf("unable to parse PEM private key: %w", err)
}
claims := &claims{
RegisteredClaims: jwt.RegisteredClaims{
Issuer: clientID,
Subject: user,
Audience: jwt.ClaimStrings{audience},
},
}
return &JWTAuthenticator{
authURL: audience + oauthTokenPath,
claims: claims,
signKey: signKey,
client: client,
logger: logger,
}, nil
}
// NewCredentials generates a new set of credentials.
func (j *JWTAuthenticator) NewCredentials() (*Credentials, error) {
// expiry needs to be set to 3 minutes or less
// See: https://help.salesforce.com/articleView?id=remoteaccess_oauth_jwt_flow.htm
j.claims.ExpiresAt = jwt.NewNumericDate(time.Now().Add(time.Minute * 3))
token := jwt.NewWithClaims(jwt.SigningMethodRS256, j.claims)
signedToken, err := token.SignedString(j.signKey)
if err != nil {
return nil, fmt.Errorf("could not sign JWT token: %w", err)
}
form := url.Values{
"grant_type": []string{grantJWT},
"assertion": []string{signedToken},
}
req, err := http.NewRequest("POST", j.authURL, strings.NewReader(form.Encode()))
if err != nil {
return nil, fmt.Errorf("could not build authentication request: %w", err)
}
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
res, err := j.client.Do(req)
if err != nil {
return nil, fmt.Errorf("could not execute authentication request: %w", err)
}
defer res.Body.Close()
if res.StatusCode >= 400 {
msg := fmt.Sprintf("received unexpected status code %d from authentication", res.StatusCode)
if resb, err := io.ReadAll(res.Body); err == nil {
msg += ": " + string(resb)
}
return nil, errors.New(msg)
}
c := &Credentials{}
err = json.NewDecoder(res.Body).Decode(c)
if err != nil {
return nil, fmt.Errorf("could not decode authentication response into credentails: %w", err)
}
return c, nil
}
// RefreshCredentials renews credentials.
func (j *JWTAuthenticator) RefreshCredentials() (*Credentials, error) {
return nil, errors.New("not implemented")
}
// CreateOrRenewCredentials will always create a new set of credentials.
func (j *JWTAuthenticator) CreateOrRenewCredentials() (*Credentials, error) {
return j.NewCredentials()
}