add RelyingParty function

This commit is contained in:
David Sharnoff 2022-07-20 18:05:17 -07:00
parent d6cbf10a00
commit 87e755647b

View file

@ -23,9 +23,7 @@ const (
pkceCode = "pkce" pkceCode = "pkce"
) )
var ( var ErrUserInfoSubNotMatching = errors.New("sub from userinfo does not match the sub from the id_token")
ErrUserInfoSubNotMatching = errors.New("sub from userinfo does not match the sub from the id_token")
)
// RelyingParty declares the minimal interface for oidc clients // RelyingParty declares the minimal interface for oidc clients
type RelyingParty interface { type RelyingParty interface {
@ -65,11 +63,9 @@ type RelyingParty interface {
type ErrorHandler func(w http.ResponseWriter, r *http.Request, errorType string, errorDesc string, state string) type ErrorHandler func(w http.ResponseWriter, r *http.Request, errorType string, errorDesc string, state string)
var ( var DefaultErrorHandler ErrorHandler = func(w http.ResponseWriter, r *http.Request, errorType string, errorDesc string, state string) {
DefaultErrorHandler ErrorHandler = func(w http.ResponseWriter, r *http.Request, errorType string, errorDesc string, state string) {
http.Error(w, errorType+": "+errorDesc, http.StatusInternalServerError) http.Error(w, errorType+": "+errorDesc, http.StatusInternalServerError)
} }
)
type relyingParty struct { type relyingParty struct {
issuer string issuer string
@ -536,3 +532,23 @@ func WithClientAssertionJWT(clientAssertion string) CodeExchangeOpt {
return client.ClientAssertionCodeOptions(clientAssertion) return client.ClientAssertionCodeOptions(clientAssertion)
} }
} }
type tokenEndpointCaller struct {
RelyingParty
}
func (t tokenEndpointCaller) TokenEndpoint() string {
return t.OAuthConfig().Endpoint.TokenURL
}
func RefreshAccessToken(rp RelyingParty, refreshToken, clientAssertion, clientAssertionType string) (*oauth2.Token, error) {
request := oidc.RefreshTokenRequest{
RefreshToken: refreshToken,
Scopes: rp.OAuthConfig().Scopes,
ClientID: rp.OAuthConfig().ClientID,
ClientSecret: rp.OAuthConfig().ClientSecret,
ClientAssertion: clientAssertion,
ClientAssertionType: clientAssertionType,
}
return client.CallTokenEndpoint(request, tokenEndpointCaller{RelyingParty: rp})
}