Skip to content
/ go-oidc Public
forked from coreos/go-oidc

A Go OpenID Connect client.

License

Notifications You must be signed in to change notification settings

pace/go-oidc

This branch is 5 commits ahead of, 18 commits behind coreos/go-oidc:v2.

Folders and files

NameName
Last commit message
Last commit date
May 2, 2017
Jul 8, 2015
Jan 23, 2020
Jun 29, 2015
Jun 29, 2015
Jun 29, 2015
Nov 1, 2018
Jun 29, 2015
Apr 20, 2017
Jan 4, 2018
Nov 14, 2016
Oct 21, 2017
Apr 26, 2017
Jul 1, 2020
Jan 24, 2020
Oct 21, 2017
Jul 1, 2020
Jul 1, 2020

Repository files navigation

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
    }
}

About

A Go OpenID Connect client.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 99.3%
  • Shell 0.7%