forked from someone1/gcp-jwt-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iam_jwt.go
56 lines (46 loc) · 1.46 KB
/
iam_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
package gcpjwt
import (
"context"
"fmt"
"strings"
"github.com/golang-jwt/jwt/v4"
"google.golang.org/api/iamcredentials/v1"
)
var (
// SigningMethodIAMJWT implements signing JWTs with the IAM signJwt API.
// https://cloud.google.com/iam/docs/reference/credentials/rest/v1/projects.serviceAccounts/signJwt
SigningMethodIAMJWT *SigningMethodIAM
)
func init() {
SigningMethodIAMJWT = &SigningMethodIAM{
alg: "IAMJWT", // NOT USED
sign: signJwt,
override: jwt.SigningMethodRS256.Alg(),
}
jwt.RegisterSigningMethod(SigningMethodIAMJWT.Alg(), func() jwt.SigningMethod {
return SigningMethodIAMJWT
})
}
func signJwt(ctx context.Context, iamService *iamcredentials.Service, config *IAMConfig, signingString string) (string, error) {
// Prepare the call
// First decode the JSON string and discard the header
parts := strings.Split(signingString, ".")
if len(parts) != 2 {
return "", fmt.Errorf("gcpjwt: expected a 2 part string to sign, got %d parts", len(parts))
}
jwtClaimSet, err := jwt.DecodeSegment(parts[1])
if err != nil {
return "", err
}
signReq := &iamcredentials.SignJwtRequest{Payload: string(jwtClaimSet)}
name := fmt.Sprintf("projects/-/serviceAccounts/%s", config.ServiceAccount)
// Do the call
signResp, err := iamService.Projects.ServiceAccounts.SignJwt(name, signReq).Context(ctx).Do()
if err != nil {
return "", err
}
config.Lock()
defer config.Unlock()
config.lastKeyID = signResp.KeyId
return signResp.SignedJwt, nil
}