From ee38ef96dfa6409bca1edc37d9ab63c27d3adcec Mon Sep 17 00:00:00 2001 From: jwijenbergh Date: Wed, 27 Sep 2023 15:37:23 +0200 Subject: Client + Server + OAuth: Support mobile redirects --- internal/oauth/oauth.go | 30 +++++++++++++++++++++++------- internal/server/server.go | 8 ++++---- 2 files changed, 27 insertions(+), 11 deletions(-) (limited to 'internal') diff --git a/internal/oauth/oauth.go b/internal/oauth/oauth.go index 4a873f1..6de3ac7 100644 --- a/internal/oauth/oauth.go +++ b/internal/oauth/oauth.go @@ -446,7 +446,7 @@ func (oauth *OAuth) ListenerPort() (int, error) { } // AuthURL gets the authorization url to start the OAuth procedure. -func (oauth *OAuth) AuthURL(name string, postProcessAuth func(string) string) (string, error) { +func (oauth *OAuth) AuthURL(name string, postProcessAuth func(string) string, cr string) (string, error) { // Update the client ID oauth.ClientID = name @@ -478,10 +478,14 @@ func (oauth *OAuth) AuthURL(name string, postProcessAuth func(string) string) (s return "", errors.WrapPrefix(err, "oauth.setupListener error", 0) } - // Get the listener port - port, err := oauth.ListenerPort() - if err != nil { - return "", errors.WrapPrefix(err, "oauth.ListenerPort error", 0) + red := cr + if cr == "" { + // Get the listener port + port, err := oauth.ListenerPort() + if err != nil { + return "", errors.WrapPrefix(err, "oauth.ListenerPort error", 0) + } + red = fmt.Sprintf("http://127.0.0.1:%d/callback", port) } params := map[string]string{ @@ -491,7 +495,7 @@ func (oauth *OAuth) AuthURL(name string, postProcessAuth func(string) string) (s "response_type": "code", "scope": "config", "state": state, - "redirect_uri": fmt.Sprintf("http://127.0.0.1:%d/callback", port), + "redirect_uri": red, } p, err := url.Parse(oauth.BaseAuthorizationURL) @@ -510,13 +514,25 @@ func (oauth *OAuth) AuthURL(name string, postProcessAuth func(string) string) (s return postProcessAuth(u), nil } +func (oauth *OAuth) tokensWithURI(ctx context.Context, uri string) error { + // parse URI + p, err := url.Parse(uri) + if err != nil { + return err + } + return oauth.tokenHandler(ctx, p) +} + // Exchange starts the OAuth exchange by getting the tokens with the redirect callback // If it was unsuccessful it returns an error. -func (oauth *OAuth) Exchange(ctx context.Context) error { +func (oauth *OAuth) Exchange(ctx context.Context, uri string) error { // If there is no HTTP client defined, create a new one if oauth.httpClient == nil { oauth.httpClient = httpw.NewClient() } + if uri != "" { + return oauth.tokensWithURI(ctx, uri) + } return oauth.tokensWithCallback(ctx) } diff --git a/internal/server/server.go b/internal/server/server.go index b6f3b30..1bdef28 100644 --- a/internal/server/server.go +++ b/internal/server/server.go @@ -49,12 +49,12 @@ func UpdateTokens(srv Server, t oauth.Token) { srv.OAuth().UpdateTokens(t) } -func OAuthURL(srv Server, name string) (string, error) { - return srv.OAuth().AuthURL(name, srv.TemplateAuth()) +func OAuthURL(srv Server, name string, cr string) (string, error) { + return srv.OAuth().AuthURL(name, srv.TemplateAuth(), cr) } -func OAuthExchange(ctx context.Context, srv Server) error { - return srv.OAuth().Exchange(ctx) +func OAuthExchange(ctx context.Context, srv Server, uri string) error { + return srv.OAuth().Exchange(ctx, uri) } func HeaderToken(ctx context.Context, srv Server) (string, error) { -- cgit v1.2.3