|
| 1 | +/* |
| 2 | +Package gcp-jwt-go has basic implementations of using Google Cloud KMS, Google IAM API (both signJwt and signBlob), |
| 3 | +and the App Identity API from AppEngine Standard to sign JWT tokens using the package |
| 4 | +"github.com/dgrijalva/jwt-go". Should work across virtually all environments, on or off of Google's Cloud Platform. |
| 5 | +
|
| 6 | +Getting Started |
| 7 | +
|
| 8 | +It is highly reccomended that you override the default algorithm implementations that you want to leverage a GCP service |
| 9 | +for in dgrijalva/jwt-go. You otherwise will have to manually pick the verification method for your JWTs and they will |
| 10 | +place non-standard headers in the rendered JWT (with the exception of signJwt from the IAM API which overwrites the |
| 11 | +header with its own). |
| 12 | +
|
| 13 | +You should only need to override the algorithm(s) you plan to use. It is also incorrect to override overlapping, |
| 14 | +algorithms such as `gcpjwt.SigningMethodKMSRS256.Override()` and `gcpjwt.SigningMethodIAMJWT.Override()` |
| 15 | +
|
| 16 | +Example: |
| 17 | +
|
| 18 | + import ( |
| 19 | + "github.com/someone1/gcp-jwt-go" |
| 20 | + ) |
| 21 | +
|
| 22 | + func init() { |
| 23 | + // Pick one or more of the following to override |
| 24 | +
|
| 25 | + // Cloud KMS |
| 26 | + gcpjwt.SigningMethodKMSRS256.Override() // RS256 |
| 27 | + gcpjwt.SigningMethodKMSPS256.Override() // PS256 |
| 28 | + gcpjwt.SigningMethodKMSES256.Override() // ES256 |
| 29 | + gcpjwt.SigningMethodKMSES384.Override() // ES384 |
| 30 | +
|
| 31 | + // IAM API - This implements RS256 exclusively |
| 32 | + gcpjwt.SigningMethodIAMJWT.Override() // For signJwt |
| 33 | + gcpjwt.SigningMethodIAMBlob.Override() // For signBlob |
| 34 | +
|
| 35 | + // AppEngine - Standard runtime only, does not apply to Flexible runtime, implements RS256 exclusively |
| 36 | + // You can also use any of the above methods on AppEngine Standard |
| 37 | + gcpjwt.SigningMethodAppEngine.Override() |
| 38 | + } |
| 39 | +
|
| 40 | +As long as a you override a default algorithm implementation as shown above, using the dgrijalva/jwt-go is mostly unchanged. |
| 41 | +
|
| 42 | +Create a Token |
| 43 | +
|
| 44 | +Token creation is more/less done the same way as in the dgrijalva/jwt-go package. The key that you need to provide is |
| 45 | +always going to be a context.Context, usuaully with a configuration object loaded in: |
| 46 | + - use gcpjwt.IAMConfig for the SigningMethodIAMJWT and SigningMethodIAMBlob signing methods |
| 47 | + - use an appengine.NewContext for the SigningMethodAppEngine signing method |
| 48 | + - use gcpjwt.KMSConfig for any of the KMS signing methods |
| 49 | +
|
| 50 | +Example: |
| 51 | +
|
| 52 | +
|
| 53 | + import ( |
| 54 | + "context" |
| 55 | + "net/http" |
| 56 | +
|
| 57 | + "github.com/dgrijalva/jwt-go" |
| 58 | + "github.com/someone1/gcp-jwt-go" |
| 59 | + "google.golang.org/appengine" // only on AppEngine Standard when using the SigningMethodAppEngine signing method |
| 60 | + ) |
| 61 | +
|
| 62 | + func makeToken(ctx context.Context) (string, error) string { |
| 63 | + // Important - if on AppEngine standard, even if you aren't useing the SigningMethodAppEngine signing method |
| 64 | + // you must pass around the appengine.NewContext context as it is required for the API calls all methods must |
| 65 | + // make. |
| 66 | +
|
| 67 | + var key interface{} |
| 68 | + claims := &jwt.StandardClaims{ |
| 69 | + ExpiresAt: 15000, |
| 70 | + Issuer: "test", |
| 71 | + } |
| 72 | + token := jwt.NewWithClaims(gcpjwt.SigningMethodGCPJWT, claims) |
| 73 | +
|
| 74 | + // Prepare your signing key |
| 75 | +
|
| 76 | + // For SigningMethodIAMJWT or SigningMethodIAMBlob |
| 77 | + config := &gcpjwt.IAMConfig{ |
| 78 | + ServiceAccount: "[email protected]", |
| 79 | + IAMType: gcpjwt.IAMJwtType, // or gcpjwt.IAMBlobType |
| 80 | + } |
| 81 | + key = gcpjwt.NewIAMContext(ctx, config) |
| 82 | +
|
| 83 | + // For any KMS signing method |
| 84 | + config := &gcpjwt.KMSConfig{ |
| 85 | + KeyPath: "name=projects/<project-id>/locations/<location>/keyRings/<key-ring-name>/cryptoKeys/<key-name>/cryptoKeyVersions/<key-version>", |
| 86 | + } |
| 87 | + key = gcpjwt.NewKMSContext(ctx, config) |
| 88 | +
|
| 89 | + // For SigningMethodAppEngine |
| 90 | + key = ctx |
| 91 | +
|
| 92 | + // For all signing methods EXCEPT signJWT |
| 93 | + return token.SignedString(key) |
| 94 | +
|
| 95 | + // For signJwt |
| 96 | + // !!IMPORTANT!! Due to the way the signJwt API returns tokens, we can't use the standard signing process |
| 97 | + // to sign |
| 98 | + signingString, err := token.SigningString() |
| 99 | + if err != nil { |
| 100 | + return "", err |
| 101 | + } |
| 102 | +
|
| 103 | + return token.Method.Sign(signingString, key) |
| 104 | + } |
| 105 | +
|
| 106 | +Validate a Token |
| 107 | +
|
| 108 | +Finally, the steps to validate a token should be straight forward. This library provides you with helper jwt.Keyfunc |
| 109 | +implementations to do the heavy lifting around getting the public certificates for verification: |
| 110 | +
|
| 111 | + - gcpjwt.IAMVerfiyKeyfunc can be used for the IAM API and the AppEngine Standard signing methods |
| 112 | + - gcpjwt.AppEngineVerfiyKeyfunc is only available on AppEngine standard and can only be used on JWT signed from the same default service account as the running application |
| 113 | + - gcp.KMSVerfiyKeyfunc can be used for the Cloud KMS signing methods |
| 114 | +
|
| 115 | +Example: |
| 116 | +
|
| 117 | + import ( |
| 118 | + "context" |
| 119 | + "time" |
| 120 | + "strings" |
| 121 | +
|
| 122 | + "github.com/dgrijalva/jwt-go" |
| 123 | + "github.com/someone1/gcp-jwt-go" |
| 124 | + ) |
| 125 | +
|
| 126 | + func validateToken(ctx context.Context, tokenString string) (*jwt.Token, error) { |
| 127 | + // Important - if on AppEngine standard, even if you aren't useing the SigningMethodAppEngine signing method |
| 128 | + // you must pass around the appengine.NewContext context as it is required for the API calls all methods must |
| 129 | + // make. |
| 130 | +
|
| 131 | + var keyFunc jwt.Keyfunc |
| 132 | +
|
| 133 | + // Prepare your key function |
| 134 | +
|
| 135 | + // For SigningMethodIAMJWT or SigningMethodIAMBlob or SigningMethodAppEngine |
| 136 | + config := &gcpjwt.IAMConfig{ |
| 137 | + ServiceAccount: "[email protected]", |
| 138 | + IAMType: gcpjwt.IAMJwtType, // or gcpjwt.IAMBlobType (use the Blob type if you used the SigningMethodAppEngine before) |
| 139 | + EnableCache: true, // Enable the certificate cache so we don't fetch it on every verification - RECOMMENDED |
| 140 | + } |
| 141 | + keyFunc = gcpjwt.IAMVerfiyKeyfunc(ctx, config) |
| 142 | +
|
| 143 | + // For any KMS signing method |
| 144 | + config := &gcpjwt.KMSConfig{ |
| 145 | + KeyPath: "name=projects/<project-id>/locations/<location>/keyRings/<key-ring-name>/cryptoKeys/<key-name>/cryptoKeyVersions/<key-version>", |
| 146 | + } |
| 147 | + keyFunc = gcpjwt.KMSVerfiyKeyfunc(ctx, config) |
| 148 | +
|
| 149 | + // For SigningMethodAppEngine only on AppEngine Standard |
| 150 | + keyFunc = gcpjwt.AppEngineVerfiyKeyfunc(ctx, true, time.Hour) |
| 151 | +
|
| 152 | + // If you called an Override function as recommended above, for both signing and verifying a token, you can use |
| 153 | + // the regular verification steps - and the same goes if you did NOT call it for both signing and verifying (using non-standard 'alg' headers) |
| 154 | + // EXCEPT for the signJwt IAM API signing method which overwrites the header's alg to RS256 |
| 155 | + return jwt.Parse(tokenString, keyFunc) |
| 156 | +
|
| 157 | + // The following is an extreme and advanced use-case - it is NOT recommended but here for those who need it. |
| 158 | + // |
| 159 | + // If we need to manually override the detected jwt.SigningMethod based on the 'alg' header |
| 160 | + // This is basically copying the https://github.com/dgrijalva/jwt-go/blob/master/parser.go#L23 ParseWithClaims function here but forcing our own method vs getting one based on the Alg field |
| 161 | + // Or Try and parse, Ignore the result and try with the proper method: |
| 162 | + token, _ := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) { |
| 163 | + return nil, nil |
| 164 | + }) |
| 165 | + parts := strings.Split(token.Raw, ".") |
| 166 | + token.Method = gcpjwt.SigningMethodIAMJWT // or whichever method you want to force |
| 167 | + if err := token.Method.Verify(strings.Join(parts[0:2], "."), token.Signature, keyFunc); err != nil { |
| 168 | + return nil, err |
| 169 | + } else { |
| 170 | + token.Valid = true |
| 171 | + } |
| 172 | + return token, nil |
| 173 | + } |
| 174 | +*/ |
| 175 | +package gcpjwt |
0 commit comments