package
0.0.0-20190718021401-6345b638bfc9
Repository: https://github.com/emanoelxavier/openid2go.git
Documentation: pkg.go.dev

# README

Go OpenId

godoc license

Summary

A Go package that implements web service middlewares for authenticating identities represented by OpenID Connect (OIDC) ID Tokens.

"OpenID Connect 1.0 is a simple identity layer on top of the OAuth 2.0 protocol. It enables Clients to verify the identity of the End-User based on the authentication performed by an Authorization Server" - OpenID Connect

Installation

go get github.com/emanoelxavier/openid2go/openid

Example

This example demonstrates how to use this package to validate incoming ID Tokens. It initializes the Configuration with the desired providers (OPs) and registers two middlewares: openid.Authenticate and openid.AuthenticateUser. The former performs the token validation while the latter, in addition to that, will forward the user information to the next handler.

import (
	"fmt"
	"net/http"

	"github.com/emanoelxavier/openid2go/openid"
)

func AuthenticatedHandler(w http.ResponseWriter, r *http.Request) {
	fmt.Fprintln(w, "The user was authenticated!")
}

func AuthenticatedHandlerWithUser(u *openid.User, w http.ResponseWriter, r *http.Request) {
	fmt.Fprintf(w, "The user was authenticated! The token was issued by %v and the user is %+v.", u.Issuer, u)
}

func Example() {
	configuration, err := openid.NewConfiguration(openid.ProvidersGetter(myGetProviders))

	if err != nil {
		panic(err)
	}
	
	http.Handle("/user", openid.AuthenticateUser(configuration, openid.UserHandlerFunc(AuthenticatedHandlerWithUser)))
	http.Handle("/authn", openid.Authenticate(configuration, http.HandlerFunc(AuthenticatedHandler)))
	
	http.ListenAndServe(":5100", nil)
}

func myGetProviders() ([]openid.Provider, error) {
	provider, err := openid.NewProvider("https://providerissuer", []string{"myClientID"})

	if err != nil {
		return nil, err
	}

	return []openid.Provider{provider}, nil
}

This example is also available in the documentation of this package, for more details see GoDoc.

Additional examples:

Tests

Unit Tests

go test github.com/emanoelxavier/openid2go/openid

Integration Tests

In addition to to unit tests, this package also comes with integration tests that will validate real ID Tokens issued by real OIDC providers. The following command will run those tests:

go test -tags integration github.com/emanoelxavier/openid2go/openid -issuer=[issuer] -clientID=[clientID] -idToken=[idToken]

Replace [issuer], [clientID] and [idToken] with the information from an identity provider of your choice.

For a quick spin you can use it with tokens issued by Google for the Google OAuth PlayGround entering "openid" (without quotes) within the scope field and copying the issued ID Token. For this provider and client the values will be:

go test -tags integration github.com/emanoelxavier/openid2go/openid -issuer=https://accounts.google.com -clientID=407408718192.apps.googleusercontent.com -idToken=copiedIDToken

Contributing

  1. Open an issue if found a bug or have a functional request.
  2. Disccuss.
  3. Branch off, write the fix with test(s) and commit attaching to the issue.
  4. Make a pull request.

# Functions

Authenticate middleware performs the validation of the OIDC ID Token.
AuthenticateUser middleware performs the validation of the OIDC ID Token and forwards the authenticated user's information to the next handler in the pipeline.
ErrorHandler option registers the function responsible for handling the errors returned during token validation.
HTTPGetter option registers the function responsible for returning the providers containing the valid issuer and client IDs used to validate the ID Token.
NewConfiguration creates a new instance of Configuration and returns a pointer to it.
NewProvider returns a new instance of a Provider created with the given issuer and clientIDs.
ProvidersGetter option registers the function responsible for returning the providers containing the valid issuer and client IDs used to validate the ID Token.

# Constants

Empty collection of providers provided during setup.
Invalid client id collection provided during setup.
Invalid issuer provided during setup.
Unexpected token audience value.
Authorization header not found on request.
Authorization header unexpected format.
Authorization header unexpected scheme.
Failure while decoding the jwk set.
Failure while decoding the OIDC configuration.
Empty jwk returned.
Empty jwk key set returned.
Empty collection of providers.
Failure while retrieving jwk set.
Failure while retrieving the OIDC configuration.
Empty ID token.
Unexpected token audience content.
Unexpected token audience type.
Unexpected token issuer content.
Unexpected token issuer type.
Unexpected token subject content.
Unexpected token subject type.
Unexpected token value.
Jwt token validation failed with a known error.
Jwt token validation failed with an unknown error.
Key identifier not found.
Error while marshalling the signing key.
Token missing the 'sub' claim.

# Structs

The Configuration contains the entities needed to perform ID token validation.
Provider represents an OpenId Identity Provider (OP) and contains the information needed to perform validation of ID Token.
SetupError represents the error returned by operations called during middleware setup.
User represents the authenticated user encapsulating information obtained from the validated ID token.
ValidationError represents the error returned by operations called during token validation.

# Interfaces

The UserHandler represents a handler to be registered by the middleware AuthenticateUser.

# Type aliases

The ErrorHandlerFunc represents the function used to handle errors during token validation.
GetIDTokenFunc represents the function used to provide the OIDC idToken.
The GetProvidersFunc defines the function type used to retrieve the collection of allowed OP(s) along with the respective client IDs registered with those providers that can access the backend service using this package.
HTTPGetFunc is a function that gets a URL based on a contextual request and a target URL.
SetupErrorCode is the type of error code that can be returned by the operations done during middleware setup.
The UserHandlerFunc is an adapter to allow the use of functions as UserHandler.
ValidationErrorCode is the type of error code that can be returned by the operations done during token validation.