authentication

package
v0.0.0-...-bf55f72 Latest Latest
Warning

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

Go to latest
Published: Apr 9, 2025 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ApiKey

type ApiKey struct {
	Key string
}

ApiKey uses an API key that authorize and authenticate the requests.

The API key is used in the `X-goog-api-key` header of the request.

See more: https://cloud.google.com/docs/authentication/api-keys-use#using-with-rest

func NewApiKey

func NewApiKey(key string) *ApiKey

func (*ApiKey) Delete

func (k *ApiKey) Delete(url string) ([]byte, error)

Delete do a HTTP DELETE request with the API key is set in the `X-goog-api-key` header of the request.

Returns the body bytes.

If response status code is not 200, returns the body as a git.gotrbe.io/go/google/errors.Error.

func (*ApiKey) Get

func (k *ApiKey) Get(url string) ([]byte, error)

Get do a HTTP Get request with the API key is set in the `X-goog-api-key` header of the request.

Returns the body bytes.

If response status code is not 200, returns the body as a git.gotrbe.io/go/google/errors.Error.

func (*ApiKey) Post

func (k *ApiKey) Post(url, contentType string, body io.Reader) ([]byte, error)

Post do a HTTP Post request with the API key is set in the `X-goog-api-key` header of the request.

Returns the body bytes.

If response status code is not 200, returns the body as a git.gotrbe.io/go/google/errors.Error.

func (*ApiKey) Put

func (k *ApiKey) Put(url string, contentType string, body io.Reader) ([]byte, error)

Put do a HTTP PUT request with the API key is set in the `X-goog-api-key` header of the request.

Returns the body bytes.

If response status code is not 204, returns the body as a git.gotrbe.io/go/google/errors.Error.

func (*ApiKey) SetScopes

func (k *ApiKey) SetScopes(scope ...string)

SetScopes does nothing for ApiKey. This function just needed to comply with the Authenticator interface.

type Authenticator

type Authenticator interface {
	// Get method should return the body bytes.
	// If the status code is not 200, then this method should return the body as [git.gorbe.io/go/google/errors.Error].
	Get(url string) ([]byte, error)

	// Post method should return the body bytes.
	// If the status code is not 200, then this method should return the body as [git.gorbe.io/go/google/errors.Error].
	Post(url, contentType string, body io.Reader) ([]byte, error)

	// Delete method should return the body bytes.
	// If the status code is not 200, then this method should return the body as [git.gorbe.io/go/google/errors.Error].
	Delete(url string) ([]byte, error)

	// Put method should return the body bytes.
	// If the status code is not 200, then this method should return the body as [git.gorbe.io/go/google/errors.Error].
	Put(url string, contentType string, body io.Reader) ([]byte, error)

	// SetScopes method sets the scope informations for OAuth2 authentication.
	// For ApiKey, this method do nothing.
	SetScopes(scope ...string)
}

Authenticator is an interface that can do authenticated "GET" and "POST" requests.

type ServiceAccount

type ServiceAccount struct {
	Email        string   `json:"client_email" yaml:"Email"`
	PrivateKey   string   `json:"private_key" yaml:"PrivateKey"`
	PrivateKeyID string   `json:"private_key_id" yaml:"PrivateKeyID"`
	TokenURL     string   `json:"token_uri" yaml:"TokenURL"`
	Scopes       []string `json:"scopes" yaml:"Scopes"`
	Subject      string   `json:"subject" yaml:"Subject"`
}

ServiceAccount uses a Google Developers service account JSON key to read the credentials that authorize and authenticate the requests.

func ServiceAccountFromJSON

func ServiceAccountFromJSON(data []byte) (*ServiceAccount, error)

ServiceAccountFromJSON uses a Google Developers service account JSON key to read the credentials that authorize and authenticate the requests.

func ServiceAccountFromJSONFile

func ServiceAccountFromJSONFile(name string) (*ServiceAccount, error)

ServiceAccountFromJSONFile uses a Google Developers service account JSON key file in path name to read the credentials that authorize and authenticate the requests.

func (*ServiceAccount) Delete

func (sa *ServiceAccount) Delete(url string) ([]byte, error)

Delete do a HTTP DELETE request with the credentials stored in *ServiceAccount.

Returns the body bytes.

If response status code is not 200, returns the body as a git.gotrbe.io/go/google/errors.Error.

func (*ServiceAccount) Get

func (sa *ServiceAccount) Get(url string) ([]byte, error)

Get do a HTTP GET request with the credentials stored in *ServiceAccount.

Returns the body bytes.

If response status code is not 200, returns the body as a git.gotrbe.io/go/google/errors.Error.

func (*ServiceAccount) Name

func (sa *ServiceAccount) Name() string

Name returns the username part of Email.

Example
package main

import (
	"fmt"

	"git.gorbe.io/go/google/authentication"
)

func main() {

	sa := new(authentication.ServiceAccount)
	sa.Email = "user0123@projectname-012345.iam.gserviceaccount.com"

	fmt.Printf("%s\n", sa.Name())
}
Output:

user0123

func (*ServiceAccount) Post

func (sa *ServiceAccount) Post(url, contentType string, body io.Reader) ([]byte, error)

Post do a HTTP POST request with the credentials stored in *ServiceAccount.

Returns the body bytes.

If response status code is not 200, returns the body as a git.gotrbe.io/go/google/errors.Error.

func (*ServiceAccount) Put

func (sa *ServiceAccount) Put(url string, contentType string, body io.Reader) ([]byte, error)

Put do a HTTP PUT request with the credentials stored in *ServiceAccount.

Returns the body bytes.

If response status code is not 204, returns the body as a git.gotrbe.io/go/google/errors.Error.

func (*ServiceAccount) SetScopes

func (sa *ServiceAccount) SetScopes(scope ...string)

SetScopes sets the OAuth2 scope information.

Jump to

Keyboard shortcuts

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