Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logout functionality through provider #226

Open
wants to merge 2 commits into
base: v2
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 30 additions & 5 deletions oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"io/ioutil"
"mime"
"net/http"
"net/url"
"strings"
"time"

Expand Down Expand Up @@ -69,6 +70,7 @@ type Provider struct {
authURL string
tokenURL string
userInfoURL string
logoutURL string

// Raw claims returned by the server.
rawClaims []byte
Expand All @@ -82,11 +84,12 @@ type cachedKeys struct {
}

type providerJSON struct {
Issuer string `json:"issuer"`
AuthURL string `json:"authorization_endpoint"`
TokenURL string `json:"token_endpoint"`
JWKSURL string `json:"jwks_uri"`
UserInfoURL string `json:"userinfo_endpoint"`
Issuer string `json:"issuer"`
AuthURL string `json:"authorization_endpoint"`
TokenURL string `json:"token_endpoint"`
JWKSURL string `json:"jwks_uri"`
UserInfoURL string `json:"userinfo_endpoint"`
EndSessionURL string `json:"end_session_endpoint"`
}

// NewProvider uses the OpenID Connect discovery mechanism to construct a Provider.
Expand Down Expand Up @@ -128,6 +131,7 @@ func NewProvider(ctx context.Context, issuer string) (*Provider, error) {
authURL: p.AuthURL,
tokenURL: p.TokenURL,
userInfoURL: p.UserInfoURL,
logoutURL: p.EndSessionURL,
rawClaims: body,
remoteKeySet: NewRemoteKeySet(ctx, p.JWKSURL),
}, nil
Expand Down Expand Up @@ -158,6 +162,27 @@ func (p *Provider) Endpoint() oauth2.Endpoint {
return oauth2.Endpoint{AuthURL: p.authURL, TokenURL: p.tokenURL}
}

// LogoutURL returns the logout endpoints for the given provider.
// See: https://openid.net/specs/openid-connect-session-1_0.html#RPLogout
func (p *Provider) LogoutURL(idTokenHint string, redirectURL string, state string) string {
logoutURL, err := url.Parse(p.logoutURL)
if err != nil {
return ""
}
query := logoutURL.Query()
if idTokenHint != "" {
query.Set("id_token_hint", idTokenHint)
}
if redirectURL != "" {
query.Set("post_logout_redirect_uri", redirectURL)
}
if state != "" {
query.Set("state", state)
}
logoutURL.RawQuery = query.Encode()
return logoutURL.String()
}

// UserInfo represents the OpenID Connect userinfo claims.
type UserInfo struct {
Subject string `json:"sub"`
Expand Down