add JWTAccessTokenFromKey
This commit is contained in:
Родитель
9f5d4180bd
Коммит
41ee26a353
|
@ -66,7 +66,7 @@ type jwtAccess struct {
|
||||||
ts oauth2.TokenSource
|
ts oauth2.TokenSource
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewJwtAccessFromFile(keyFile string, audience string) (credentials.Credentials, error) {
|
func NewJWTAccessFromFile(keyFile string, audience string) (credentials.Credentials, error) {
|
||||||
jsonKey, err := ioutil.ReadFile(keyFile)
|
jsonKey, err := ioutil.ReadFile(keyFile)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("credentials: failed to read the service account key file: %v", err)
|
return nil, fmt.Errorf("credentials: failed to read the service account key file: %v", err)
|
||||||
|
@ -78,6 +78,14 @@ func NewJwtAccessFromFile(keyFile string, audience string) (credentials.Credenti
|
||||||
return jwtAccess{ts: ts}, nil
|
return jwtAccess{ts: ts}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewJWTAccessFromKey(jsonKey []byte, audience string) (credentials.Credentials, error) {
|
||||||
|
ts, err := google.JWTAccessTokenSourceFromJSON(jsonKey, audience)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return jwtAccess{ts: ts}, nil
|
||||||
|
}
|
||||||
|
|
||||||
func (j jwtAccess) GetRequestMetadata(ctx context.Context) (map[string]string, error) {
|
func (j jwtAccess) GetRequestMetadata(ctx context.Context) (map[string]string, error) {
|
||||||
token, err := j.ts.Token()
|
token, err := j.ts.Token()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -340,7 +340,7 @@ func doServiceAccountCreds(tc testpb.TestServiceClient) {
|
||||||
grpclog.Println("ServiceAccountCreds done")
|
grpclog.Println("ServiceAccountCreds done")
|
||||||
}
|
}
|
||||||
|
|
||||||
func doJwtTokenCreds(tc testpb.TestServiceClient) {
|
func doJWTTokenCreds(tc testpb.TestServiceClient) {
|
||||||
pl := newPayload(testpb.PayloadType_COMPRESSABLE, largeReqSize)
|
pl := newPayload(testpb.PayloadType_COMPRESSABLE, largeReqSize)
|
||||||
req := &testpb.SimpleRequest{
|
req := &testpb.SimpleRequest{
|
||||||
ResponseType: testpb.PayloadType_COMPRESSABLE.Enum(),
|
ResponseType: testpb.PayloadType_COMPRESSABLE.Enum(),
|
||||||
|
@ -357,7 +357,7 @@ func doJwtTokenCreds(tc testpb.TestServiceClient) {
|
||||||
if !strings.Contains(string(jsonKey), user) {
|
if !strings.Contains(string(jsonKey), user) {
|
||||||
grpclog.Fatalf("Got user name %q which is NOT a substring of %q.", user, jsonKey)
|
grpclog.Fatalf("Got user name %q which is NOT a substring of %q.", user, jsonKey)
|
||||||
}
|
}
|
||||||
grpclog.Println("JwttokenCreds done")
|
grpclog.Println("JWTtokenCreds done")
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
@ -440,7 +440,7 @@ func main() {
|
||||||
}
|
}
|
||||||
opts = append(opts, grpc.WithPerRPCCredentials(jwtCreds))
|
opts = append(opts, grpc.WithPerRPCCredentials(jwtCreds))
|
||||||
} else if *testCase == "jwt_token_creds" {
|
} else if *testCase == "jwt_token_creds" {
|
||||||
jwtCreds, err := oauth.NewJwtAccessFromFile(*serviceAccountKeyFile, "https://"+*serverHost+":"+string(*serverPort)+"/"+"TestService")
|
jwtCreds, err := oauth.NewJWTAccessFromFile(*serviceAccountKeyFile, "https://"+*serverHost+":"+string(*serverPort)+"/"+"TestService")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
grpclog.Fatalf("Failed to create JWT credentials: %v", err)
|
grpclog.Fatalf("Failed to create JWT credentials: %v", err)
|
||||||
}
|
}
|
||||||
|
@ -482,7 +482,7 @@ func main() {
|
||||||
if !*useTLS {
|
if !*useTLS {
|
||||||
grpclog.Fatalf("TLS is not enabled. TLS is required to execute service_account_creds test case.")
|
grpclog.Fatalf("TLS is not enabled. TLS is required to execute service_account_creds test case.")
|
||||||
}
|
}
|
||||||
doJwtTokenCreds(tc)
|
doJWTTokenCreds(tc)
|
||||||
case "cancel_after_begin":
|
case "cancel_after_begin":
|
||||||
doCancelAfterBegin(tc)
|
doCancelAfterBegin(tc)
|
||||||
case "cancel_after_first_response":
|
case "cancel_after_first_response":
|
||||||
|
|
Загрузка…
Ссылка в новой задаче