2019-02-18 14:57:54 +01:00
|
|
|
/*
|
2019-05-24 15:13:15 +02:00
|
|
|
Copyright (c) JSC iCore.
|
2019-02-18 14:57:54 +01:00
|
|
|
|
2019-05-24 15:13:15 +02:00
|
|
|
This source code is licensed under the MIT license found in the
|
|
|
|
LICENSE file in the root directory of this source tree.
|
2019-02-18 14:57:54 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
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 {
|
2021-05-13 07:40:27 +02:00
|
|
|
hydraURL string
|
2021-08-06 14:30:48 +02:00
|
|
|
fakeTLSTermination bool
|
2021-05-13 07:40:27 +02:00
|
|
|
rememberFor int
|
2019-02-18 14:57:54 +01:00
|
|
|
}
|
|
|
|
|
2019-05-15 14:03:05 +02:00
|
|
|
// NewConsentReqDoer creates a ConsentRequest.
|
2021-08-06 14:30:48 +02:00
|
|
|
func NewConsentReqDoer(hydraURL string, fakeTLSTermination bool, rememberFor int) *ConsentReqDoer {
|
|
|
|
return &ConsentReqDoer{hydraURL: hydraURL, fakeTLSTermination: fakeTLSTermination, 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) {
|
2021-08-06 14:30:48 +02:00
|
|
|
ri, err := initiateRequest(consent, crd.hydraURL, crd.fakeTLSTermination, challenge)
|
2019-02-18 14:57:54 +01:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
2021-08-06 14:30:48 +02:00
|
|
|
redirectURI, err := acceptRequest(consent, crd.hydraURL, crd.fakeTLSTermination, challenge, data)
|
2019-02-18 14:57:54 +01:00
|
|
|
return redirectURI, errors.Wrap(err, "failed to accept consent request")
|
|
|
|
}
|