go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/server/auth/deprecated/common_test.go (about) 1 // Copyright 2015 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package deprecated 16 17 import ( 18 "context" 19 "crypto/rsa" 20 "encoding/base64" 21 "encoding/binary" 22 "encoding/json" 23 "fmt" 24 25 "go.chromium.org/luci/server/auth/openid" 26 "go.chromium.org/luci/server/auth/signing/signingtest" 27 ) 28 29 func jwksForTest(keyID string, pubKey *rsa.PublicKey) *openid.JSONWebKeySetStruct { 30 modulus := pubKey.N.Bytes() 31 exp := []byte{0, 0, 0, 0} 32 binary.BigEndian.PutUint32(exp, uint32(pubKey.E)) 33 return &openid.JSONWebKeySetStruct{ 34 Keys: []openid.JSONWebKeyStruct{ 35 { 36 Kty: "RSA", 37 Alg: "RS256", 38 Use: "sig", 39 Kid: keyID, 40 N: base64.RawURLEncoding.EncodeToString(modulus), 41 E: base64.RawURLEncoding.EncodeToString(exp), 42 }, 43 }, 44 } 45 } 46 47 func jwtForTest(ctx context.Context, body []byte, keyID string, signer *signingtest.Signer) string { 48 b64hdr := base64.RawURLEncoding.EncodeToString([]byte( 49 fmt.Sprintf(`{"alg": "RS256","kid": "%s"}`, keyID))) 50 b64bdy := base64.RawURLEncoding.EncodeToString(body) 51 _, sig, err := signer.SignBytes(ctx, []byte(b64hdr+"."+b64bdy)) 52 if err != nil { 53 panic(err) 54 } 55 return b64hdr + "." + b64bdy + "." + base64.RawURLEncoding.EncodeToString(sig) 56 } 57 58 func idTokenForTest(ctx context.Context, tok *openid.IDToken, keyID string, signer *signingtest.Signer) string { 59 body, err := json.Marshal(tok) 60 if err != nil { 61 panic(err) 62 } 63 return jwtForTest(ctx, body, keyID, signer) 64 }