hydra-werther/internal/hydra/consent.go

52 lines
1.6 KiB
Go
Raw Normal View History

2019-02-18 14:57:54 +01:00
/*
Copyright (C) JSC iCore - All Rights Reserved
Unauthorized copying of this file, via any medium is strictly prohibited
Proprietary and confidential
*/
package hydra
import (
"github.com/pkg/errors"
)
// ConsentReqDoer fetches information on the OAuth2 request and then accept or reject the requested authentication process.
type ConsentReqDoer struct {
2019-05-15 14:03:05 +02:00
hydraURL string
rememberFor int
2019-02-18 14:57:54 +01:00
}
2019-05-15 14:03:05 +02:00
// NewConsentReqDoer creates a ConsentRequest.
func NewConsentReqDoer(hydraURL string, rememberFor int) *ConsentReqDoer {
return &ConsentReqDoer{hydraURL: hydraURL, rememberFor: rememberFor}
2019-02-18 14:57:54 +01:00
}
// InitiateRequest fetches information on the OAuth2 request.
2019-05-15 14:03:05 +02:00
func (crd *ConsentReqDoer) InitiateRequest(challenge string) (*ReqInfo, error) {
2019-02-18 14:57:54 +01:00
ri, err := initiateRequest(consent, crd.hydraURL, challenge)
return ri, errors.Wrap(err, "failed to initiate consent request")
}
2019-05-15 14:03:05 +02:00
// AcceptConsentRequest accepts the requested authentication process, and returns redirect URI.
func (crd *ConsentReqDoer) AcceptConsentRequest(challenge string, remember bool, grantScope []string, idToken interface{}) (string, error) {
2019-02-18 14:57:54 +01:00
type session struct {
IDToken interface{} `json:"id_token,omitempty"`
}
data := struct {
GrantScope []string `json:"grant_scope"`
Remember bool `json:"remember"`
RememberFor int `json:"remember_for"`
Session session `json:"session,omitempty"`
}{
GrantScope: grantScope,
Remember: remember,
2019-05-15 14:03:05 +02:00
RememberFor: crd.rememberFor,
2019-02-18 14:57:54 +01:00
Session: session{
IDToken: idToken,
},
}
redirectURI, err := acceptRequest(consent, crd.hydraURL, challenge, data)
return redirectURI, errors.Wrap(err, "failed to accept consent request")
}