jade

package module
v0.0.0-...-d1ad28d Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2025 License: MIT Imports: 10 Imported by: 0

README

Jade Authentication Project

A Go-based authentication project utilizing the Jade library to provide a seamless authentication experience.

Overview

This project aims to demonstrate the usage of the Jade library for authentication purposes. It includes examples for various authentication providers, such as Google, GitHub, and custom providers.

Features

  • Authentication with multiple providers (Google, GitHub, etc.)
  • Custom provider implementation
  • Session management using JadeStore
  • Middleware for checking user login status

Installation

To install the project, run the following command:

go get github.com/djsisson/jade

Usage

To use the project, import the jade package and use the provided functions for authentication and session management.

import "github.com/djsisson/jade"

Examples

The project includes several examples to demonstrate the usage of the Jade library:

  • examples/basic: A basic example using Google authentication
  • examples/custom_provider: An example using a custom authentication provider
  • examples/custom_store: An example using a custom session store

Getting Started

To run the examples, navigate to the respective directory and execute the main.go file using the go run command.

License

This project is licensed under the MIT License. See LICENSE for more information.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ClearProviders

func ClearProviders()

ClearProviders removes all registered providers from the global providers map.

func NewOIDCProvider

func NewOIDCProvider[T Claims](opts *OIDCOptions) (*oidcProvider[T], error)

newOIDCProvider creates a new provider for the given issuer and options.

This function wraps the go-oidc NewProvider function and the newProvider function in this package. It takes an OIDCOptions struct and returns an oidcProvider, which is a Provider that wraps an *oidc.Provider and can be used to authenticate users.

The returned Provider will have the given name, scopes, and auth options. The EndPoint field of the Provider's ProviderOptions will be set to the Provider's Endpoint().

func NewProvider

func NewProvider[T Claims](opts *ProviderOptions) (*provider[T], error)

func RemoveProvider

func RemoveProvider(name string)

func UseProviders

func UseProviders(p ...Provider)

UseProviders sets the providers that can be used to authenticate with the server. If a provider with the same name already exists, it will be overwritten.

func WithForcedApprovalPrompt

func WithForcedApprovalPrompt() oauth2.AuthCodeOption

WithForcedApprovalPrompt returns an AuthCodeOption that forces the user to approve the request every time, even if they previously approved a request with the same client_id and scope.

func WithHostedDomain

func WithHostedDomain(hd string) oauth2.AuthCodeOption

WithHostedDomain returns an AuthCodeOption that adds the "hd" parameter to the authorize URL. The value of the parameter is the given domain.

See https://developers.google.com/identity/protocols/OpenIDConnect#hd-param

func WithIdentityProvider

func WithIdentityProvider(idp string) oauth2.AuthCodeOption

WithIdentityProvider returns an AuthCodeOption that adds the "identity_provider" parameter to the authorize URL, which can be used to specify the identity provider that the user should use to authenticate.

See https://developers.google.com/identity/protocols/OpenIDConnect#hd-param

func WithLoginHint

func WithLoginHint(loginHint string) oauth2.AuthCodeOption

WithLoginHint returns an AuthCodeOption that adds the "login_hint" parameter to the authorize URL. The value of the parameter is the given login hint.

See https://developers.google.com/identity/protocols/OpenIDConnect#login_hint

func WithNonce

func WithNonce(nonce string) oauth2.AuthCodeOption

WithNonce returns an AuthCodeOption that adds the "nonce" parameter to the authorize URL. The value of the parameter is the given nonce.

See https://developers.google.com/identity/protocols/OpenIDConnect#nonce

func WithOfflineAccess

func WithOfflineAccess() oauth2.AuthCodeOption

WithOfflineAccess returns an AuthCodeOption that requests a refresh token that can be used to obtain a new access token when the user is not present.

See https://developers.google.com/identity/protocols/OpenIDConnect#offline

func WithOnlineAccess

func WithOnlineAccess() oauth2.AuthCodeOption

func WithPrompt

func WithPrompt(prompt ...string) oauth2.AuthCodeOption

WithPrompt returns an AuthCodeOption that adds the "prompt" parameter to the authorize URL. The value of the parameter is the space-separated list of given prompts.

See https://developers.google.com/identity/protocols/OpenIDConnect#promptrelatedparameters

func WithScopes

func WithScopes(scopes ...string) oauth2.AuthCodeOption

WithScopes returns an AuthCodeOption that adds the "scope" parameter to the authorize URL. The value of the parameter is the space-separated list of given scopes.

See https://developers.google.com/identity/protocols/OpenIDConnect#scopes

Types

type Address

type Address struct {
	Formatted     string `json:"formatted,omitempty"`
	StreetAddress string `json:"street_address,omitempty"`
	Locality      string `json:"locality,omitempty"`
	Region        string `json:"region,omitempty"`
	PostalCode    string `json:"postal_code,omitempty"`
	Country       string `json:"country,omitempty"`
}

type AuthCode

type AuthCode struct {
	Url   string
	Code  string
	Nonce string
}

type Claims

type Claims interface {
	MarshalToUser() *User
}

type OIDCOptions

type OIDCOptions struct {
	Options
	AuthOptions []oauth2.AuthCodeOption
	Issuer      string
	UseNonce    bool
	UsePKCE     bool
}

type Options

type Options struct {
	Name         string
	ClientID     string
	ClientSecret string
	CallbackURL  string
	Scopes       []string
}

type ParseIDTokenOptions

type ParseIDTokenOptions struct {
	SkipAccessTokenCheck bool
	AccessToken          string
}

type Provider

type Provider interface {
	Name() string
	AuthCodeURL(state string, opts ...oauth2.AuthCodeOption) *AuthCode
	GetOAuthToken(code string, opts ...oauth2.AuthCodeOption) (*Token, error)
	GetUserData(ctx context.Context, token *Token) (*User, error)
	IsTokenValid(ctx context.Context, token *Token) bool
}

func GetProvider

func GetProvider(name string) (Provider, error)

GetProvider returns the provider with the given name. If no provider with the given name exists, it returns an error.

type ProviderOptions

type ProviderOptions struct {
	Options
	EndPoint    oauth2.Endpoint
	AuthOptions []oauth2.AuthCodeOption
	ApiURL      string
	UsePKCE     bool
	Claims      Claims
}

type Providers

type Providers map[string]Provider

func GetProviders

func GetProviders() Providers

GetProviders returns a map of all providers that have been registered. The map is keyed by the name of the provider.

type Token

type Token struct {
	*oauth2.Token
	*oidc.IDToken
}

type User

type User struct {
	ID            string  `json:"sub,omitempty"`
	Issuer        string  `json:"iss,omitempty"`
	Name          string  `json:"name,omitempty"`
	Email         string  `json:"email,omitempty"`
	EmailVerified bool    `json:"email_verified,omitempty"`
	FirstName     string  `json:"given_name,omitempty"`
	LastName      string  `json:"family_name,omitempty"`
	MiddleName    string  `json:"middle_name,omitempty"`
	Nickname      string  `json:"nickname,omitempty"`
	UserName      string  `json:"preferred_username,omitempty"`
	Gender        string  `json:"gender,omitempty"`
	Birthdate     string  `json:"birthdate,omitempty"`
	Profile       string  `json:"profile,omitempty"`
	Picture       string  `json:"picture,omitempty"`
	Zoneinfo      string  `json:"zoneinfo,omitempty"`
	Locale        string  `json:"locale,omitempty"`
	UpdatedAt     int64   `json:"updated_at,omitempty"`
	WebSite       string  `json:"website,omitempty"`
	Phone         string  `json:"phone_number,omitempty"`
	Address       Address `json:"address"`
}

Directories

Path Synopsis
examples
basic command
custom_provider command
custom_store command

Jump to

Keyboard shortcuts

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