forked from someone1/gcp-jwt-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathiam_blob.go
53 lines (43 loc) · 1.33 KB
/
iam_blob.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
package gcpjwt
import (
"context"
"encoding/base64"
"fmt"
"github.com/golang-jwt/jwt/v4"
"google.golang.org/api/iamcredentials/v1"
)
var (
// SigningMethodIAMBlob implements signing JWTs with the IAM signBlob API.
// https://cloud.google.com/iam/docs/reference/credentials/rest/v1/projects.serviceAccounts/signBlob
SigningMethodIAMBlob *SigningMethodIAM
)
func init() {
SigningMethodIAMBlob = &SigningMethodIAM{
alg: "IAMBlob",
sign: signBlob,
override: jwt.SigningMethodRS256.Alg(),
}
jwt.RegisterSigningMethod(SigningMethodIAMBlob.Alg(), func() jwt.SigningMethod {
return SigningMethodIAMBlob
})
}
func signBlob(ctx context.Context, iamService *iamcredentials.Service, config *IAMConfig, signingString string) (string, error) {
// Prepare the call
signReq := &iamcredentials.SignBlobRequest{
Payload: base64.StdEncoding.EncodeToString([]byte(signingString)),
}
name := fmt.Sprintf("projects/-/serviceAccounts/%s", config.ServiceAccount)
// Do the call
signResp, err := iamService.Projects.ServiceAccounts.SignBlob(name, signReq).Context(ctx).Do()
if err != nil {
return "", err
}
config.Lock()
defer config.Unlock()
config.lastKeyID = signResp.KeyId
signature, err := base64.StdEncoding.DecodeString(signResp.SignedBlob)
if err != nil {
return "", err
}
return jwt.EncodeSegment(signature), nil
}