summaryrefslogtreecommitdiff
path: root/internal
diff options
context:
space:
mode:
authorjwijenbergh <jeroenwijenbergh@protonmail.com>2022-09-08 14:53:02 +0200
committerjwijenbergh <jeroenwijenbergh@protonmail.com>2022-09-08 14:53:02 +0200
commiteb21880077d59e5b5fd7aac3353f4c8cc6bcba43 (patch)
tree79411849d098ab3804d451f924c45741b8dfbef9 /internal
parent7f7b6884d11e0e2b891814b84eb906db284a50b0 (diff)
OAuth: Separate login functions to get rid of callback
Diffstat (limited to 'internal')
-rw-r--r--internal/oauth/oauth.go34
-rw-r--r--internal/server/common.go8
2 files changed, 13 insertions, 29 deletions
diff --git a/internal/oauth/oauth.go b/internal/oauth/oauth.go
index 59d0061..89854f7 100644
--- a/internal/oauth/oauth.go
+++ b/internal/oauth/oauth.go
@@ -254,18 +254,18 @@ func (oauth *OAuth) Init(baseAuthorizationURL string, tokenURL string) {
}
// Starts the OAuth exchange for eduvpn.
-func (oauth *OAuth) start(name string, postProcessAuth func(string) string, doAuth func(string) error) error {
+func (oauth *OAuth) GetAuthURL(name string, postProcessAuth func(string) string) (string, error) {
errorMessage := "failed starting OAuth exchange"
// Generate the state
state, stateErr := genState()
if stateErr != nil {
- return &types.WrappedErrorMessage{Message: errorMessage, Err: stateErr}
+ return "", &types.WrappedErrorMessage{Message: errorMessage, Err: stateErr}
}
// Generate the verifier and challenge
verifier, verifierErr := genVerifier()
if verifierErr != nil {
- return &types.WrappedErrorMessage{Message: errorMessage, Err: verifierErr}
+ return "", &types.WrappedErrorMessage{Message: errorMessage, Err: verifierErr}
}
challenge := genChallengeS256(verifier)
@@ -282,23 +282,19 @@ func (oauth *OAuth) start(name string, postProcessAuth func(string) string, doAu
authURL, urlErr := httpw.HTTPConstructURL(oauth.BaseAuthorizationURL, parameters)
if urlErr != nil {
- return &types.WrappedErrorMessage{Message: errorMessage, Err: urlErr}
+ return "", &types.WrappedErrorMessage{Message: errorMessage, Err: urlErr}
}
// Fill the struct with the necessary fields filled for the next call to getting the HTTP client
oauthSession := OAuthExchangeSession{ClientID: name, State: state, Verifier: verifier}
oauth.Session = oauthSession
- // Run the auth callback with the authurl processed
- doAuthErr := doAuth(postProcessAuth(authURL))
- if doAuthErr != nil {
- return &types.WrappedErrorMessage{Message: errorMessage, Err: urlErr}
- }
- return nil
+ // Return the url processed
+ return postProcessAuth(authURL), nil
}
// Error definitions
-func (oauth *OAuth) Finish() error {
+func (oauth *OAuth) Exchange() error {
tokenErr := oauth.getTokensWithCallback()
if tokenErr != nil {
@@ -315,22 +311,6 @@ func (oauth *OAuth) Cancel() {
oauth.Session.Server.Shutdown(oauth.Session.Context)
}
-func (oauth *OAuth) Login(name string, postprocessAuth func(string) string, doAuth func(string) error) error {
- errorMessage := "failed OAuth login"
- authInitializeErr := oauth.start(name, postprocessAuth, doAuth)
-
- if authInitializeErr != nil {
- return &types.WrappedErrorMessage{Message: errorMessage, Err: authInitializeErr}
- }
-
- oauthErr := oauth.Finish()
-
- if oauthErr != nil {
- return &types.WrappedErrorMessage{Message: errorMessage, Err: oauthErr}
- }
- return nil
-}
-
func (oauth *OAuth) EnsureTokens() error {
errorMessage := "failed ensuring OAuth tokens"
// Access Token or Refresh Tokens empty, we can not ensure the tokens
diff --git a/internal/server/common.go b/internal/server/common.go
index 801c778..64b8079 100644
--- a/internal/server/common.go
+++ b/internal/server/common.go
@@ -334,8 +334,12 @@ func ShouldRenewButton(server Server) bool {
return true
}
-func Login(server Server, doAuth func(string) error) error {
- return server.GetOAuth().Login("org.eduvpn.app.linux", server.GetTemplateAuth(), doAuth)
+func GetOAuthURL(server Server, name string) (string, error) {
+ return server.GetOAuth().GetAuthURL(name, server.GetTemplateAuth())
+}
+
+func OAuthExchange(server Server) error {
+ return server.GetOAuth().Exchange()
}
func GetHeaderToken(server Server) string {