GOAuth2

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jan 17, 2021 License: GPL-3.0 Imports: 12 Imported by: 0

README

GOAuth2

Go Reference Coverage Status Travis (.com)

A simple go library for getting Google OAuth2 token

Examples

Using :

package main

import (
	"fmt"
	"io/ioutil"
	"os"

	oauth "github.com/anthonyme00/GOAuth2"
)

func main() {
	connectionData := oauth.OAuthAPIConnectionData{
		ClientId:     "YOUR_CLIENT_ID",
		ClientSecret: "YOUR_CLIENT_SECRET",
		Scopes:       []string{"https://www.googleapis.com/auth/drive.file"},
	}

	token, err := oauth.GetOAuth2Token(connectionData)
	if err != nil {
		panic(err)
	}
	fmt.Printf("%+v\n", token)

	//Using token in a query
	query := UrlQuery { 
		"access_token":	token.GetAccessToken(),
		"other_query":	"aquery",
	}
	DummyGoogleAPI.Call(query)
}

Refreshing tokens :

token, err := oauth.GetOAuth2Token(connectionData)

//Automatic token refreshing when getting access token
//3 seconds threshold before actual expiration of token
current_token := token.GetAccessToken(3))

//Manual token refreshing
token.Refresh()

//Checking if token is expired
if token.IsExpired(0) {
	fmt.Println("My token is expired...")
}

Serializing and saving to a file :

key := []byte("SUPER_SECRET_KEY")

//Save Token
//You can use the non encrypted version with token.Serialize()
encryptedToken, _ := token.SerializeEncrypted(key)
ioutil.WriteFile("mytoken.dat", encryptedToken, os.FileMode(os.O_CREATE|os.O_TRUNC))

//Load Token
//To load non encrypted token, use token.Deserialize()
tokenToDecrypt, _ := ioutil.ReadFile("mytoken.dat")
decryptedToken := oauth.OAuth2Token{}
decryptedToken.DeserializeEncrypted(tokenToDecrypt, key)
fmt.Printf("%+v\n", token)
Misc

Google API Scopes : https://developers.google.com/identity/protocols/oauth2/scopes

Google OAuth2 Docs : https://developers.google.com/identity/protocols/oauth2

Documentation

Index

Constants

View Source
const OAuth2AuthEndpoint = "https://accounts.google.com/o/oauth2/v2/auth"
View Source
const OAuth2TokenEndpoint = "https://oauth2.googleapis.com/token"

Variables

This section is empty.

Functions

This section is empty.

Types

type OAuth2Token

type OAuth2Token struct {
	AccessToken    string  `json:"access_token"`
	RefreshToken   string  `json:"refresh_token"`
	Expiration     float64 `json:"expires_in"`
	IdToken        string  `json:"id_token"`
	Scope          string  `json:"scope"`
	TokenType      string  `json:"token_type"`
	LastRefresh    time.Time
	ConnectionData OAuthAPIConnectionData
}

The Token data. Use the AccessToken to access Google Apps APIs.

func GetOAuth2Token

func GetOAuth2Token(data OAuthAPIConnectionData) (*OAuth2Token, error)

Get an OAuth2 Token with the connection data specified

func (*OAuth2Token) Deserialize

func (token *OAuth2Token) Deserialize(data []byte) error

Deserialize the token.

Use DeserializeEncrypted for safety.

func (*OAuth2Token) DeserializeEncrypted

func (token *OAuth2Token) DeserializeEncrypted(data []byte, key []byte) error

Deserialize the encrypted token with a key.

Key must be <= 32 bytes.

func (*OAuth2Token) GetAccessToken

func (token *OAuth2Token) GetAccessToken(expirationThreshold float64) string

Get the access token, refreshing it automatically if it is expired.

expirationThreshold - Threshold before actual expiration in seconds after which the token will count as being expired, even if not actually expired

func (*OAuth2Token) IsExpired

func (token *OAuth2Token) IsExpired(expirationThreshold float64) bool

Used to see if the Token is expired. You can use this with Refresh() to refresh the token. Or just use GetAccessToken.

expirationThreshold - Threshold before actual expiration in seconds after which the token will count as being expired, even if not actually expired

func (*OAuth2Token) Refresh

func (token *OAuth2Token) Refresh()

Call this to refresh the token.

func (*OAuth2Token) Scopes

func (token *OAuth2Token) Scopes() []string

Used to get scopes as a string slice.

func (*OAuth2Token) Serialize

func (token *OAuth2Token) Serialize() ([]byte, error)

Serialize the token to a byte slice.

Use SerializeEncrypted for security.

func (*OAuth2Token) SerializeEncrypted

func (token *OAuth2Token) SerializeEncrypted(key []byte) ([]byte, error)

Serialize the token while encrypting it with a key.

Key must be <= 32 bytes.

type OAuthAPIConnectionData

type OAuthAPIConnectionData struct {
	ClientId     string
	ClientSecret string
	Scopes       []string
}

The connection data used to connect to Google's OAuth2 API. ClientId and ClientSecret can be taken from your developer dashboard.

https://developers.google.com/identity/protocols/oauth2/scopes

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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