hydra-werther/internal/hydra/consent.go

53 lines
1.8 KiB
Go
Raw Normal View History

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
fakeTlsTermination bool
rememberFor int
2019-02-18 14:57:54 +01:00
}
2019-05-15 14:03:05 +02:00
// NewConsentReqDoer creates a ConsentRequest.
2021-05-13 07:40:27 +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-05-13 07:40:27 +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-05-13 07:45:37 +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")
}