From 88e11acd3f399031bd0a8966e5495c2d3af77618 Mon Sep 17 00:00:00 2001 From: Sebastian Wilzbach Date: Sun, 4 Jun 2017 01:40:16 +0200 Subject: [PATCH 1/2] login(): indicate current state (present session vs. redirect) --- source/oauth/webapp.d | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/source/oauth/webapp.d b/source/oauth/webapp.d index a58dbcd..79bb2da 100644 --- a/source/oauth/webapp.d +++ b/source/oauth/webapp.d @@ -82,8 +82,11 @@ class OAuthWebapp settings = The OAuth settings that apply to this _login attempt scopes = (Optional) An array of identifiers specifying the scope of the authorization requested. + Returns: `true` if a OAuth session was obtained and + `false` if no OAuth session is present and a redirect to an + OAuth provider will happen. +/ - void login( + bool login( scope HTTPServerRequest req, scope HTTPServerResponse res, immutable OAuthSettings settings, @@ -100,6 +103,8 @@ class OAuthWebapp auto session = settings.userSession( req.session, req.query["state"], req.query["code"]); + + return true; } else { @@ -107,17 +112,18 @@ class OAuthWebapp req.session = res.startSession(); res.redirect(settings.userAuthUri(req.session, extraParams, scopes)); + return false; } } /// ditto - void login( + bool login( scope HTTPServerRequest req, scope HTTPServerResponse res, immutable OAuthSettings settings, in string[] scopes) @safe { - login(req, res, settings, null, scopes); + return login(req, res, settings, null, scopes); } /++ From f257fcd9a8f63956870376fdf89f56282a840736 Mon Sep 17 00:00:00 2001 From: Sebastian Wilzbach Date: Sun, 4 Jun 2017 01:40:49 +0200 Subject: [PATCH 2/2] Make sure that a session is properly initialized and added to req.context --- source/oauth/webapp.d | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/source/oauth/webapp.d b/source/oauth/webapp.d index 79bb2da..5eddd6a 100644 --- a/source/oauth/webapp.d +++ b/source/oauth/webapp.d @@ -25,6 +25,19 @@ class OAuthWebapp { version (Have_vibe_d_web) @noRoute: + private OAuthSession loadSessionToContext(scope HTTPServerRequest req, immutable OAuthSettings settings) @safe + in { + assert(settings !is null, "Settings can't be null"); + } + body + { + auto session = OAuthSession.load(settings, req.session); + () @trusted { + req.context["oauth.session"] = session; + } (); + return session; + } + /++ Check if a request is from a logged in user @@ -49,13 +62,9 @@ class OAuthWebapp if (!req.session) return false; - if (auto session = - settings ? OAuthSession.load(settings, req.session) : null) + if (settings !is null) { - () @trusted { - req.context["oauth.session"] = session; - } (); - + loadSessionToContext(req, settings); return true; } @@ -137,12 +146,13 @@ class OAuthWebapp Params: req = the request to get the relevant session for + settings = The OAuth settings that apply to this _login attempt Returns: The session associated to req, or `null` if no session was found. +/ final - OAuthSession oauthSession(scope HTTPServerRequest req) nothrow @trusted + OAuthSession oauthSession(scope HTTPServerRequest req, immutable OAuthSettings settings = null) nothrow @trusted in { try @@ -153,15 +163,18 @@ class OAuthWebapp body { try + { if (auto pCM = "oauth.session" in req.context) return pCM.get!OAuthSession; + else + return loadSessionToContext(req, settings); + } catch (Exception e) { import vibe.core.log : logError; logError("OAuth: Exception occurred while reading request " ~ "context: %s", e.toString()); } - return null; } }