-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from emahiro/feature/rm_local_acccesstoken
ローカルのアクセストークンの取得実装を削除する
- Loading branch information
Showing
4 changed files
with
50 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,12 @@ | ||
package aehcl | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"os" | ||
|
||
"cloud.google.com/go/compute/metadata" | ||
"golang.org/x/oauth2/google" | ||
) | ||
|
||
func fetchToken() (string, error) { | ||
// fetch idToken from metadata of gcp | ||
if idt, err := fetchIDToken(); err == nil { | ||
return idt, nil | ||
} | ||
|
||
// fetch accesstoken from local `GOOGLE_APPLICATION_CREDENTIALS` | ||
lat, err := fetchLocalAccessToken() | ||
if err != nil { | ||
return "", err | ||
} | ||
return lat, nil | ||
} | ||
|
||
func fetchIDToken() (string, error) { | ||
// FetchIDToken returns identity token from metadata API. | ||
func FetchIDToken() (string, error) { | ||
return metadata.Get("/instance/service-accounts/default/identity?audience=" + os.Getenv("GOOGLE_CLOUD_PROJECT")) | ||
} | ||
|
||
func fetchLocalAccessToken() (string, error) { | ||
creds, err := google.FindDefaultCredentials(context.Background()) | ||
if err != nil { | ||
return "", fmt.Errorf("failed to find default credentials. err: %v", err) | ||
|
||
} | ||
|
||
token, err := creds.TokenSource.Token() | ||
if err != nil { | ||
return "", fmt.Errorf("failed to fetch token. err: %v", err) | ||
|
||
} | ||
|
||
return token.AccessToken, nil | ||
} |