oidc

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

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

Go to latest
Published: Mar 27, 2024 License: Apache-2.0 Imports: 10 Imported by: 5

README

Go Reference

ugent-library/oidc

Package oidc aims to provide an easy-to-use way to do OpenID Connect ID token based authentication in your Go web app.

Install

go get -u github.com/ugent-library/oidc

Examples

    oidcAuth, _ := oidc.NewAuth(context.TODO(), oidc.Config{
		URL:          config.OIDC.URL,
		ClientID:     config.OIDC.ID,
		ClientSecret: config.OIDC.Secret,
		RedirectURL:  baseURL + "/auth/oidc",
		CookieName:   "oidc.state",
		CookieSecret: []byte(config.OIDC.CookieSecret),
	})

    http.HandleFunc("/login", func(w http.ResponseWriter, r *http.Request) {
        err := oidcAuth.BeginAuth(w, r)
        if err != nil {
            http.Error(w, "auth failed", http.StatusInternalServerError)
        }
    })

    http.HandleFunc("/auth/oidc", func(w http.ResponseWriter, r *http.Request) {
        claims := oidc.Claims{}
    	err := h.oidcAuth.CompleteAuth(c.Res, c.Req, &claims)
        if err != nil {
            http.Error(w, "auth failed", http.StatusInternalServerError)
    		return
    	}

        // handle successful login
    })

Documentation

Overview

Package oidc aims to provide an easy-to-use way to do OpenID Connect ID token based authentication in your Go web app.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Address

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

https://openid.net/specs/openid-connect-core-1_0.html#Address.

type Auth

type Auth struct {
	// contains filtered or unexported fields
}

func NewAuth

func NewAuth(ctx context.Context, c Config) (*Auth, error)

func (*Auth) BeginAuth

func (a *Auth) BeginAuth(w http.ResponseWriter, r *http.Request) error

func (*Auth) CompleteAuth

func (a *Auth) CompleteAuth(w http.ResponseWriter, r *http.Request, claims any) error

type Claims

type Claims struct {
	StandardClaims
	All map[string]any
}

func (*Claims) GetString

func (c *Claims) GetString(key string) string

func (*Claims) UnmarshalJSON

func (c *Claims) UnmarshalJSON(b []byte) (err error)

type Config

type Config struct {
	URL              string
	ClientID         string
	ClientSecret     string
	RedirectURL      string
	AdditionalScopes []string
	CookiePrefix     string
}

type StandardClaims

type StandardClaims struct {
	Address             *Address `json:"address,omitempty"`
	Birthdate           string   `json:"birthdate,omitempty"`
	Email               string   `json:"email,omitempty"`
	EmailVerified       bool     `json:"email_verified,omitempty"`
	FamilyName          string   `json:"family_name,omitempty"`
	Gender              string   `json:"gender,omitempty"`
	GivenName           string   `json:"given_name,omitempty"`
	Locale              string   `json:"locale,omitempty"`
	MiddleName          string   `json:"middle_name,omitempty"`
	Name                string   `json:"name,omitempty"`
	Nickname            string   `json:"nickname,omitempty"`
	PhoneNumber         string   `json:"phone_number,omitempty"`
	PhoneNumberVerified bool     `json:"phone_number_verified,omitempty"`
	Picture             string   `json:"picture,omitempty"`
	PreferredUsername   string   `json:"preferred_username,omitempty"`
	Profile             string   `json:"profile,omitempty"`
	Subject             string   `json:"sub,omitempty"`
	UpdatedAt           int64    `json:"updated_at,omitempty"`
	Website             string   `json:"website,omitempty"`
	ZoneInfo            string   `json:"zoneinfo,omitempty"`
}

https://openid.net/specs/openid-connect-core-1_0.html#StandardClaims.

Jump to

Keyboard shortcuts

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