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"
|
|
|
|
)
|
|
|
|
|
|
|
|
// LoginReqDoer fetches information on the OAuth2 request and then accept or reject the requested authentication process.
|
|
|
|
type LoginReqDoer 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
|
|
|
// NewLoginReqDoer creates a LoginRequest.
|
2021-08-06 14:30:48 +02:00
|
|
|
func NewLoginReqDoer(hydraURL string, fakeTLSTermination bool, rememberFor int) *LoginReqDoer {
|
|
|
|
return &LoginReqDoer{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 (lrd *LoginReqDoer) InitiateRequest(challenge string) (*ReqInfo, error) {
|
2021-08-06 14:30:48 +02:00
|
|
|
ri, err := initiateRequest(login, lrd.hydraURL, lrd.fakeTLSTermination, challenge)
|
2019-02-18 14:57:54 +01:00
|
|
|
return ri, errors.Wrap(err, "failed to initiate login request")
|
|
|
|
}
|
|
|
|
|
2019-05-15 14:03:05 +02:00
|
|
|
// AcceptLoginRequest accepts the requested authentication process, and returns redirect URI.
|
|
|
|
func (lrd *LoginReqDoer) AcceptLoginRequest(challenge string, remember bool, subject string) (string, error) {
|
2019-02-18 14:57:54 +01:00
|
|
|
data := struct {
|
|
|
|
Remember bool `json:"remember"`
|
|
|
|
RememberFor int `json:"remember_for"`
|
|
|
|
Subject string `json:"subject"`
|
|
|
|
}{
|
|
|
|
Remember: remember,
|
2019-05-15 14:03:05 +02:00
|
|
|
RememberFor: lrd.rememberFor,
|
2019-02-18 14:57:54 +01:00
|
|
|
Subject: subject,
|
|
|
|
}
|
2021-08-06 14:30:48 +02:00
|
|
|
redirectURI, err := acceptRequest(login, lrd.hydraURL, lrd.fakeTLSTermination, challenge, data)
|
2019-02-18 14:57:54 +01:00
|
|
|
return redirectURI, errors.Wrap(err, "failed to accept login request")
|
|
|
|
}
|