gTokenVerifier

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 23, 2024 License: BSD-3-Clause Imports: 16 Imported by: 0

README

gTokenVerifier

This module seeks to have a similarity to that found in Verify the Google ID token on your server side for other languages. It validates if the generated JWT corresponds to Google and also provides the possibility to validate if the user who issued the token is or is not from a specific domain.

Uso

Here is a basic example of how to use it:

package main

import (
	"fmt"

	gTokenVerifier "github.com/croonix/gTokenVerifier"
)

var (
    token := "XXXXXXXXXXX.XXXXXXXXXXXX.XXXXXXXXXX"
    aud := "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX.apps.googleusercontent.com"
	domain = "dev.intelligencepartner.com"
)

func main() {
	tokenInfo := gTokenVerifier.Verify(token, aud)
	if tokenInfo != nil {
		fmt.Println(tokenInfo)
	}

	tokenInfo = gTokenVerifier.VerifyByDomain(token, aud, domain)
	if tokenInfo != nil {
		fmt.Println(tokenInfo)
	}
}

Documentation

Overview

This module seeks to have a similarity to that found in Verify the Google ID token on your server side: https://developers.google.com/identity/gsi/web/guides/verify-google-id-token for other languages. It validates if the generated JWT corresponds to Google and also provides the possibility to validate if the user who issued the token is or is not from a specific domain.

This module have two functions:

  • Verify(token string, aud string) *TokenInfo
  • VerifyDomain(token string, aud string, domain string) *TokenInfo

The first function only validates if the token is from Google and the second function validates if the token is from Google and if the user is from the specified domain.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type TokenInfo

type TokenInfo struct {
	Sub           string `json:"sub"`
	Email         string `json:"email"`
	AtHash        string `json:"at_hash"`
	Aud           string `json:"aud"`
	EmailVerified bool   `json:"email_verified"`
	Name          string `json:"name"`
	GivenName     string `json:"given_name"`
	FamilyName    string `json:"family_name"`
	Picture       string `json:"picture"`
	Local         string `json:"locale"`
	Iss           string `json:"iss"`
	Azp           string `json:"azp"`
	Iat           int64  `json:"iat"`
	Exp           int64  `json:"exp"`
}

TokenInfo is the struct that contains the information of the token

func Verify

func Verify(authToken string, aud string) *TokenInfo

Verify verifies the token and returns the token info if the token is valid. Otherwise, it returns nil.

Example
package main

import (
	"fmt"

	"github.com/croonix/gTokenVerifier"
)

func main() {
	var (
		authTokenTest = ""
		audTest       = ""
	)

	tokenInfo := gTokenVerifier.Verify(authTokenTest, audTest)

	if tokenInfo != nil {
		fmt.Println(tokenInfo)
	}
}
Output:

func VerifyByDomain added in v1.0.0

func VerifyByDomain(authToken string, aud string, domain string) *TokenInfo

VerifyByDomain verifies the token and checks if the user in the JWT is from the specified domain. Otherwise, it returns nil.

Example
package main

import (
	"fmt"

	"github.com/croonix/gTokenVerifier"
)

func main() {
	var (
		authTokenTest = ""
		audTest       = ""
		audDomainTest = ""
	)

	tokenInfo := gTokenVerifier.VerifyByDomain(authTokenTest, audTest, audDomainTest)

	if tokenInfo != nil {
		fmt.Println(tokenInfo)
	}
}
Output:

Jump to

Keyboard shortcuts

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