gin_oidc

package module
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Jan 20, 2022 License: MIT Imports: 13 Imported by: 0

README

gin-oidc

gin middleware - an OIDC client for a relying party web server

example usage

  	// middleware params
 	initParams := gin_oidc.InitParams{
 		ClientId:     "xx-xxx-xxx",
 		ClientSecret: "xx-xxx-xxx",
 		Issuer:       "https://accounts.google.com/", //add '.well-known/openid-configuration' to see it's a good link
 		ClientUrl:    "http://example.domain/", //your website's url
		// "openid" is a required scope for OpenID Connect flows.
		Scopes:       []string{oidc.ScopeOpenID, "profile", "email"},
 		ErrorHandler: func(c *gin.Context) {
 			// gin_oidc pushes a new error before any "ErrorHandler" invocation
 			message := c.Errors.Last().Error()
 			// redirect to ErrorEndpoint with error message
 			redirectToErrorPage(c, "http://example2.domain/error", message)
 			// when "ErrorHandler" ends "c.Abort()" is invoked - no further handlers will be invoked
		},
		CallbackPath: "oidc-callback", // redirect url path
 		PostLogoutUrl: "http://example2.domain/",
 	}
	currOIDC := gin_oidc.New(initParams)

	// configure route
	router.GET("/logout", currOIDC.LogoutHandler)
	router.Any("/"+currOIDC.CallbackPath, currOIDC.CallbackHandler)

 	// protect all endpoint below this line
	router.Use(currOIDC.AuthHandler)

	// or protect a single endpoint
	router.GET("/protectedEndpoint", currOIDC.AuthHandler, protectedEndpointHandler)
  

If you have any questions feel free to open an issue.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RandomString

func RandomString(n int) string

Types

type InitParams

type InitParams struct {
	ClientId      string          //id from the authorization service (OIDC provider)
	ClientSecret  string          //secret from the authorization service (OIDC provider)
	ClientName    string          // a name for the Client, becomes a base path
	Issuer        url.URL         //the URL identifier for the authorization service. for example: "https://accounts.google.com" - try adding "/.well-known/openid-configuration" to the path to make sure it's correct
	ClientUrl     url.URL         //your website's/service's URL for example: "http://localhost:8081/" or "https://mydomain.com/
	Scopes        []string        //OAuth scopes. If you're unsure go with: []string{oidc.ScopeOpenID, "profile", "email"}
	ErrorHandler  gin.HandlerFunc //errors handler. for example: func(c *gin.Context) {c.String(http.StatusBadRequest, "ERROR...")}
	CallbackPath  string
	LogoutUrl     *url.URL // the logout URL at the Issuer to connect to for ending the session.  Is set automatically.
	PostLogoutUrl url.URL  //user will be redirected to this URL after he logs out (i.e. accesses the '/logout' endpoint added in 'Init()')
}

type OidcMiddleware

type OidcMiddleware struct {
	InitParams
	Verifier        *oidc.IDTokenVerifier
	Config          *oauth2.Config
	AuthHandler     gin.HandlerFunc
	LogoutHandler   gin.HandlerFunc
	CallbackHandler gin.HandlerFunc
}

func New

func New(i InitParams) *OidcMiddleware

Jump to

Keyboard shortcuts

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