util

package
v1.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2021 License: Apache-2.0 Imports: 21 Imported by: 3

Documentation

Overview

Copyright 2018 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright 2019 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright 2021 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright 2020 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright 2020 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright 2021 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright 2018 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright 2020 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright 2018 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Copyright 2020 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

Index

Constants

View Source
const CacheFileName = ".oauth2l"
View Source
const IamServiceAccountAccessTokenURL = "https://iamcredentials.googleapis.com/v1/projects/-/serviceAccounts/%s:generateAccessToken"

IamServiceAccountAccessTokenURL is used for generating accesss token for a Service Account.

View Source
const StsURL = "https://securetoken.googleapis.com/v1alpha2/identitybindingtoken"

StsURL is Google's Secure Token Service endpoint used for obtaining STS token. TODO (andyzhao): Replace with https://sts.googleapis.com/v1/token when ready.

Variables

View Source
var DefaultScope = "https://www.googleapis.com/auth/cloud-platform"
View Source
var MethodAPIKey = "apikey"
View Source
var MethodJWT = "jwt"
View Source
var MethodOAuth = "oauth"
View Source
var WebDirectory string = filepath.Join(GuessUnixHomeDir(), defaultWebPackageName)

Functions

func BuildHeader

func BuildHeader(tokenType string, token string) string

Returns the given token in standard header format.

func BuildRefreshTokenJSON added in v1.2.0

func BuildRefreshTokenJSON(refreshToken string, creds *google.Credentials) string

BuildRefreshTokenJSON attempts to construct a gcloud refresh token JSON using a refreshToken and an OAuth Client ID Credentials object. Empty string is returned if this is not possible.

func ClearCache

func ClearCache() error

func Curl

func Curl(settings *Settings, taskSettings *TaskSettings)

Fetches token with the given settings using Google Authenticator and use the token as header to make curl request.

func CurlCommand

func CurlCommand(cli string, header string, url string, extraArgs ...string)

Executes curl command with provided header and params.

func EncodeClaims added in v1.1.0

func EncodeClaims(settings *Settings) string

EncodeClaims base64 encodes supported STS claims in settings

func Fetch

func Fetch(settings *Settings, taskSettings *TaskSettings)

Fetches and prints the token in plain text with the given settings using Google Authenticator.

func FetchToken added in v1.2.0

func FetchToken(ctx context.Context, settings *Settings) (*oauth2.Token, error)

Returns a token from the given settings. Returns nil for API keys.

func FindJSONCredentials added in v1.2.0

func FindJSONCredentials(ctx context.Context, settings *Settings) (*google.Credentials, error)

FindJSONCredentials obtains credentials from settings or Application Default Credentials

func GenerateServiceAccountAccessToken added in v1.2.0

func GenerateServiceAccountAccessToken(accessToken string, serviceAccount string, scope string) (*oauth2.Token, error)

GenerateServiceAccountAccessToken generates a Service Account access token using a User access token approved for at least one of the following scopes: * https://www.googleapis.com/auth/iam * https://www.googleapis.com/auth/cloud-platform

func GuessUnixHomeDir added in v1.2.0

func GuessUnixHomeDir() string
func Header(settings *Settings, taskSettings *TaskSettings)

Fetches and prints the token in header format with the given settings using Google Authenticator.

func Info

func Info(token string) int

Fetches the information of the given token.

func InsertCache

func InsertCache(settings *Settings, token *oauth2.Token) error

func JWTTokenSource added in v1.2.0

func JWTTokenSource(ctx context.Context, settings *Settings) (oauth2.TokenSource, error)

func LookupCache

func LookupCache(settings *Settings) (*oauth2.Token, error)

func OAuthJSONTokenSource added in v1.2.0

func OAuthJSONTokenSource(ctx context.Context, settings *Settings) (oauth2.TokenSource, error)

func Reset

func Reset()

Resets the cache.

func SSOFetch

func SSOFetch(cli string, email string, scope string) (*oauth2.Token, error)

Fetches and returns OAuth access token using SSO CLI.

func StsExchange added in v1.1.0

func StsExchange(accessToken string, encodedClaims string) (*oauth2.Token, error)

Exchanges an OAuth Access Token to an Sts token with base64 encoded claims

func Test

func Test(token string) int

Tests the given token. Returns 0 for valid tokens. Otherwise returns 1.

func Web added in v1.1.0

func Web()

Runs the frontend/backend for OAuth2l Playground

func WebStop added in v1.1.0

func WebStop()

Closes the containers and removes stopped containers

Types

type CacheKey

type CacheKey struct {
	// The JSON credentials content downloaded from Google Cloud Console.
	CredentialsJSON string
	// If specified, use OAuth. Otherwise, JWT.
	Scope string
	// The audience field for JWT auth and UAT
	Audience string
	// The email used for SSO and domain-wide delegation.
	Email string
	// The Google API key
	APIKey string
	// The QuotaProject field for STS
	QuotaProject string
	// If specified, performs STS exchange on top of base OAuth
	Sts bool
	// Exchange User access token for Service Account access token.
	ServiceAccount string
}

The key struct that used to identify an auth token fetch operation.

type Settings added in v1.2.0

type Settings struct {
	// The JSON credentials content downloaded from Google Cloud Console.
	CredentialsJSON string
	// If specified, use OAuth. Otherwise, JWT.
	Scope string
	// The audience field for JWT auth
	Audience string
	// The Google API key
	APIKey string
	// This is only used for domain-wide delegation.
	// DEPRECATED
	User string
	// The email used for SSO and domain-wide delegation.
	Email string
	// A user specified project that is responsible for the request quota and
	// billing charges.
	QuotaProject string
	// AuthHandler is the AuthorizationHandler used for 3-legged OAuth flow.
	AuthHandler authhandler.AuthorizationHandler
	// State is a unique string used with AuthHandler.
	State string
	// Indicates that STS token exchange should be performed.
	Sts bool
	// Used for Service Account Impersonation.
	// Exchange User access token for Service Account access token.
	ServiceAccount string
}

An extensible structure that holds the credentials for Google API authentication.

func (Settings) AuthMethod added in v1.2.0

func (s Settings) AuthMethod() string

type TaskSettings added in v1.1.0

type TaskSettings struct {
	// AuthType determines which auth tool to use (sso vs sgauth)
	AuthType string
	// Output format for Fetch task
	Format string
	// CurlCli override for Curl task
	CurlCli string
	// Url endpoint for Curl task
	Url string
	// Extra args for Curl task
	ExtraArgs []string
	// SsoCli override for Sso task
	SsoCli string
	// Refresh expired access token in cache
	Refresh bool
}

An extensible structure that holds the settings used by different oauth2l tasks. These settings are used by oauth2l only and are not part of GUAC settings.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL