open-vault/vendor/github.com/coreos/go-oidc
Austin Gebauer a7531a11ea
Updates the JWT/OIDC auth plugin (#10919)
2021-02-16 17:21:35 -08:00
..
.gitignore Switch to go modules (#6585) 2019-04-13 03:44:06 -04:00
.travis.yml Updates the JWT/OIDC auth plugin (#10919) 2021-02-16 17:21:35 -08:00
CONTRIBUTING.md adding azure auth plugin (#4180) 2018-03-21 17:35:31 -04:00
DCO adding azure auth plugin (#4180) 2018-03-21 17:35:31 -04:00
LICENSE adding azure auth plugin (#4180) 2018-03-21 17:35:31 -04:00
MAINTAINERS Update plugin dependencies (#8371) 2020-02-18 09:55:04 -08:00
NOTICE adding azure auth plugin (#4180) 2018-03-21 17:35:31 -04:00
README.md adding azure auth plugin (#4180) 2018-03-21 17:35:31 -04:00
code-of-conduct.md updating azure plugin and deps (#4191) 2018-03-23 16:48:05 -04:00
jose.go adding azure auth plugin (#4180) 2018-03-21 17:35:31 -04:00
jwks.go updating azure plugin and deps (#4191) 2018-03-23 16:48:05 -04:00
oidc.go Updates the JWT/OIDC auth plugin (#10919) 2021-02-16 17:21:35 -08:00
test Switch to go modules (#6585) 2019-04-13 03:44:06 -04:00
verify.go Updates the JWT/OIDC auth plugin (#10919) 2021-02-16 17:21:35 -08:00

README.md

go-oidc

GoDoc Build Status

OpenID Connect support for Go

This package enables OpenID Connect support for the golang.org/x/oauth2 package.

provider, err := oidc.NewProvider(ctx, "https://accounts.google.com")
if err != nil {
    // handle error
}

// Configure an OpenID Connect aware OAuth2 client.
oauth2Config := oauth2.Config{
    ClientID:     clientID,
    ClientSecret: clientSecret,
    RedirectURL:  redirectURL,

    // Discovery returns the OAuth2 endpoints.
    Endpoint: provider.Endpoint(),

    // "openid" is a required scope for OpenID Connect flows.
    Scopes: []string{oidc.ScopeOpenID, "profile", "email"},
}

OAuth2 redirects are unchanged.

func handleRedirect(w http.ResponseWriter, r *http.Request) {
    http.Redirect(w, r, oauth2Config.AuthCodeURL(state), http.StatusFound)
}

The on responses, the provider can be used to verify ID Tokens.

var verifier = provider.Verifier(&oidc.Config{ClientID: clientID})

func handleOAuth2Callback(w http.ResponseWriter, r *http.Request) {
    // Verify state and errors.

    oauth2Token, err := oauth2Config.Exchange(ctx, r.URL.Query().Get("code"))
    if err != nil {
        // handle error
    }

    // Extract the ID Token from OAuth2 token.
    rawIDToken, ok := oauth2Token.Extra("id_token").(string)
    if !ok {
        // handle missing token
    }

    // Parse and verify ID Token payload.
    idToken, err := verifier.Verify(ctx, rawIDToken)
    if err != nil {
        // handle error
    }

    // Extract custom claims
    var claims struct {
        Email    string `json:"email"`
        Verified bool   `json:"email_verified"`
    }
    if err := idToken.Claims(&claims); err != nil {
        // handle error
    }
}