3 * Copyright 2015 gRPC authors.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
19 // Package oauth implements gRPC credentials using OAuth.
27 "golang.org/x/net/context"
29 "golang.org/x/oauth2/google"
30 "golang.org/x/oauth2/jwt"
31 "google.golang.org/grpc/credentials"
34 // TokenSource supplies PerRPCCredentials from an oauth2.TokenSource.
35 type TokenSource struct {
39 // GetRequestMetadata gets the request metadata as a map from a TokenSource.
40 func (ts TokenSource) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
41 token, err := ts.Token()
45 return map[string]string{
46 "authorization": token.Type() + " " + token.AccessToken,
50 // RequireTransportSecurity indicates whether the credentials requires transport security.
51 func (ts TokenSource) RequireTransportSecurity() bool {
55 type jwtAccess struct {
59 // NewJWTAccessFromFile creates PerRPCCredentials from the given keyFile.
60 func NewJWTAccessFromFile(keyFile string) (credentials.PerRPCCredentials, error) {
61 jsonKey, err := ioutil.ReadFile(keyFile)
63 return nil, fmt.Errorf("credentials: failed to read the service account key file: %v", err)
65 return NewJWTAccessFromKey(jsonKey)
68 // NewJWTAccessFromKey creates PerRPCCredentials from the given jsonKey.
69 func NewJWTAccessFromKey(jsonKey []byte) (credentials.PerRPCCredentials, error) {
70 return jwtAccess{jsonKey}, nil
73 func (j jwtAccess) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
74 ts, err := google.JWTAccessTokenSourceFromJSON(j.jsonKey, uri[0])
78 token, err := ts.Token()
82 return map[string]string{
83 "authorization": token.Type() + " " + token.AccessToken,
87 func (j jwtAccess) RequireTransportSecurity() bool {
91 // oauthAccess supplies PerRPCCredentials from a given token.
92 type oauthAccess struct {
96 // NewOauthAccess constructs the PerRPCCredentials using a given token.
97 func NewOauthAccess(token *oauth2.Token) credentials.PerRPCCredentials {
98 return oauthAccess{token: *token}
101 func (oa oauthAccess) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
102 return map[string]string{
103 "authorization": oa.token.Type() + " " + oa.token.AccessToken,
107 func (oa oauthAccess) RequireTransportSecurity() bool {
111 // NewComputeEngine constructs the PerRPCCredentials that fetches access tokens from
112 // Google Compute Engine (GCE)'s metadata server. It is only valid to use this
113 // if your program is running on a GCE instance.
114 // TODO(dsymonds): Deprecate and remove this.
115 func NewComputeEngine() credentials.PerRPCCredentials {
116 return TokenSource{google.ComputeTokenSource("")}
119 // serviceAccount represents PerRPCCredentials via JWT signing key.
120 type serviceAccount struct {
126 func (s *serviceAccount) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
131 s.t, err = s.config.TokenSource(ctx).Token()
136 return map[string]string{
137 "authorization": s.t.Type() + " " + s.t.AccessToken,
141 func (s *serviceAccount) RequireTransportSecurity() bool {
145 // NewServiceAccountFromKey constructs the PerRPCCredentials using the JSON key slice
146 // from a Google Developers service account.
147 func NewServiceAccountFromKey(jsonKey []byte, scope ...string) (credentials.PerRPCCredentials, error) {
148 config, err := google.JWTConfigFromJSON(jsonKey, scope...)
152 return &serviceAccount{config: config}, nil
155 // NewServiceAccountFromFile constructs the PerRPCCredentials using the JSON key file
156 // of a Google Developers service account.
157 func NewServiceAccountFromFile(keyFile string, scope ...string) (credentials.PerRPCCredentials, error) {
158 jsonKey, err := ioutil.ReadFile(keyFile)
160 return nil, fmt.Errorf("credentials: failed to read the service account key file: %v", err)
162 return NewServiceAccountFromKey(jsonKey, scope...)
165 // NewApplicationDefault returns "Application Default Credentials". For more
166 // detail, see https://developers.google.com/accounts/docs/application-default-credentials.
167 func NewApplicationDefault(ctx context.Context, scope ...string) (credentials.PerRPCCredentials, error) {
168 t, err := google.DefaultTokenSource(ctx, scope...)
172 return TokenSource{t}, nil