PTGUoauth

package
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2023 License: MIT Imports: 8 Imported by: 1

README

PTGU OAuth Google

Import

import (
	PTGUoauth "github.com/parinyapt/golang_utils/oauth/google/v1"
)

Example

Config OAuth
googleOAuth := PTGUoauth.NewGoogleOAuth(&oauth2.Config{
  RedirectURL:  "URL_TO_REDIRECT_AFTER_LOGIN",
  ClientID:     "CLIENT_ID",
  ClientSecret: "CLIENT_SECRET",
  Scopes: []string{
    "https://www.googleapis.com/auth/userinfo.email",
    "https://www.googleapis.com/auth/userinfo.profile",
  },
  Endpoint: google.Endpoint,
})
Generate Login URL
loginURL := googleOAuth.GenerateLoginURL("STATE")
Get Access Token by Code
accessToken, err := googleOAuth.GetAccessToken("CODE")
if err != nil {
  panic(err)
}
Get Token Info by Access Token
tokenInfo, validateStatus, err := googleOAuth.GetTokenInfo(accessToken)
if err != nil {
  panic(err)
}
fmt.Println(tokenInfo.AUD)
fmt.Println(tokenInfo.UserID)
fmt.Println(tokenInfo.Email)
fmt.Println(validateStatus.Aud)
fmt.Println(validateStatus.Exp)
Get User Info by Access Token
userInfo, err := googleOAuth.GetUserInfo(accessToken)
if err != nil {
  panic(err)
}
fmt.Println(userInfo.UserID)
fmt.Println(userInfo.Email)
fmt.Println(userInfo.Name)
fmt.Println(userInfo.Picture)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewGoogleOAuth

func NewGoogleOAuth(inputConfig *oauth2.Config) *googleOAuthReceiverArgument

Types

type GoogleOAuthMethod

type GoogleOAuthMethod interface {
	// GenerateOAuthURL is a function to generate oauth url for user to login
	GenerateOAuthURL(state string) (oauthURL string)

	// GetAccessToken is a function to get user access token by code that response from google
	GetAccessToken(code string) (accessToken string, err error)

	// GetTokenInfo is a function to get token info from google server
	GetTokenInfo(accessToken string) (returnData ReturnGoogleGetTokenInfo, validateStatus ReturnGoogleValidateStatusGetTokenInfo, err error)

	// GetUserInfo is a function to get user info from google server
	GetUserInfo(accessToken string) (returnData ReturnGoogleGetUserInfo, err error)
}

type ReturnGoogleGetTokenInfo

type ReturnGoogleGetTokenInfo struct {
	UserID        string `json:"sub"`
	AZP           string `json:"azp"`
	AUD           string `json:"aud"`
	Scope         string `json:"scope"`
	Exp           string `json:"exp"`
	ExpiresIn     string `json:"expires_in"`
	Email         string `json:"email"`
	EmailVerified string `json:"email_verified"`
	AccessType    string `json:"access_type"`
}

type ReturnGoogleGetUserInfo

type ReturnGoogleGetUserInfo struct {
	UserID        string `json:"id"`
	Email         string `json:"email"`
	VerifiedEmail bool   `json:"verified_email"`
	Name          string `json:"name"`
	FirstName     string `json:"given_name"`
	LastName      string `json:"family_name"`
	Picture       string `json:"picture"`
	Locale        string `json:"locale"`
}

type ReturnGoogleValidateStatusGetTokenInfo

type ReturnGoogleValidateStatusGetTokenInfo struct {
	Aud bool
	Exp bool
}

Jump to

Keyboard shortcuts

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