From cf21ff8033cdda524d30ff1cc911dfdfe650c8d1 Mon Sep 17 00:00:00 2001 From: Jacob Jensen Date: Sat, 21 Oct 2017 20:23:47 +0200 Subject: [PATCH] Added response to authentication And also fixed routes only making first character to lower. --- src/controllers/authentication.d | 17 +++++++++++---- src/controllers/controller.d | 36 +++++++++++++++++++++++--------- src/http/route.d | 24 ++++++++++++++++++--- 3 files changed, 60 insertions(+), 17 deletions(-) diff --git a/src/controllers/authentication.d b/src/controllers/authentication.d index 23e8716..9f524aa 100644 --- a/src/controllers/authentication.d +++ b/src/controllers/authentication.d @@ -9,7 +9,7 @@ import diamond.core.apptype; static if (isWeb) { - import vibe.d : HTTPServerRequest; + import vibe.d : HTTPServerRequest, HTTPServerResponse; /// Wrapper for an authentication status. final class AuthStatus @@ -18,6 +18,9 @@ static if (isWeb) /// The request. HTTPServerRequest _httpRequest; + /// The response. + HTTPServerResponse _httpResponse; + /// Boolean determining whether the authentication was successful or not. bool _authenticated; @@ -29,12 +32,14 @@ static if (isWeb) * Creates a new authentcation status. * Params: * request = The request that was authenticated. + * response = The response for the request. * authenticated = Boolean determining whehter the authentication was successful or not. * message = (optional) The message of the authentication status. */ - this(HTTPServerRequest request, bool authenticated, string message = null) + this(HTTPServerRequest request, HTTPServerResponse response, bool authenticated, string message = null) { _httpRequest = request; + _httpResponse = response; _authenticated = authenticated; _message = message; } @@ -44,6 +49,9 @@ static if (isWeb) /// Gets the request that was authenticated. HTTPServerRequest httpRequest() { return _httpRequest; } + /// Gets the response for the request. + HTTPServerResponse httpResponse() { return _httpResponse; } + /// Gets a boolean determining whether the authentication was successful or not. bool authenticated() { return _authenticated; } @@ -58,11 +66,12 @@ static if (isWeb) /** * Function called to validate authentication for a request. * Params: - * request = The request to validate for authentication. + * request = The request to validate for authentication. + * response = The response for the authentication. * Returns: * True if the request is authenticated. */ - AuthStatus isAuthenticated(HTTPServerRequest request); + AuthStatus isAuthenticated(HTTPServerRequest request, HTTPServerResponse response); /** * Function called when authentication fails. diff --git a/src/controllers/controller.d b/src/controllers/controller.d index ee8833a..fb3422e 100644 --- a/src/controllers/controller.d +++ b/src/controllers/controller.d @@ -9,7 +9,7 @@ import diamond.core.apptype; static if (isWeb) { - import std.string : strip, format, toLower; + import std.string : strip, format; import std.traits : hasUDA, getUDAs; import vibe.d; @@ -21,6 +21,24 @@ static if (isWeb) import diamond.controllers.authentication; import diamond.core.collections; + private string firstToLower(string s) + { + import std.string : toLower; + import std.conv : to; + + if (!s) + { + return s; + } + + if (s.length == 1) + { + return s.toLower(); + } + + return to!string(s[0]).toLower() ~ s[1 .. $]; + } + /// The format used for default mappings. enum defaultMappingFormat = q{ static if (hasUDA!(%s.%s, HttpDefault)) @@ -48,7 +66,7 @@ static if (isWeb) ( action_%s.action && action_%s.action.strip().length ? action_%s.action : "%s" - ).toLower(), + ).firstToLower(), &controller.%s ); } @@ -68,7 +86,7 @@ static if (isWeb) ( action_%s.action && action_%s.action.strip().length ? action_%s.action : "%s" - ).toLower() + ).firstToLower() ); } } @@ -214,7 +232,7 @@ static if (isWebServer) { if (_auth && !_disabledAuth["/"]) { - auto authStatus = _auth.isAuthenticated(view.httpRequest); + auto authStatus = _auth.isAuthenticated(view.httpRequest, view.httpResponse); if (!authStatus || !authStatus.authenticated) { @@ -257,7 +275,7 @@ static if (isWebServer) if (_auth && !_disabledAuth[_view.route.action]) { - auto authStatus = _auth.isAuthenticated(view.httpRequest); + auto authStatus = _auth.isAuthenticated(view.httpRequest, view.httpResponse); if (!authStatus || !authStatus.authenticated) { @@ -440,7 +458,7 @@ else static if (isWebApi) { if (_auth && !_disabledAuth["/"]) { - auto authStatus = _auth.isAuthenticated(httpRequest); + auto authStatus = _auth.isAuthenticated(httpRequest, httpResponse); if (!authStatus || !authStatus.authenticated) { @@ -483,7 +501,7 @@ else static if (isWebApi) if (_auth && !_disabledAuth[route.action]) { - auto authStatus = _auth.isAuthenticated(httpRequest); + auto authStatus = _auth.isAuthenticated(httpRequest, httpResponse); if (!authStatus || !authStatus.authenticated) { @@ -528,12 +546,10 @@ else static if (isWebApi) auto controllerCollectionResult = ""; - import std.string : toLower; - foreach (controller; controllerInitializers) { import std.string : format; - controllerCollectionResult ~= format(generateFormat, controller.toLower(), controller); + controllerCollectionResult ~= format(generateFormat, controller.firstToLower(), controller); } return controllerCollectionResult; diff --git a/src/http/route.d b/src/http/route.d index 7c6b22a..acf7ed1 100644 --- a/src/http/route.d +++ b/src/http/route.d @@ -13,6 +13,24 @@ static if (isWeb) import diamond.errors : enforce; + private string firstToLower(string s) + { + import std.string : toLower; + import std.conv : to; + + if (!s) + { + return s; + } + + if (s.length == 1) + { + return s.toLower(); + } + + return to!string(s[0]).toLower() ~ s[1 .. $]; + } + // A http route. final class Route { @@ -37,7 +55,7 @@ static if (isWeb) { enforce(url && url.length, "Invalid route url."); - import std.string : strip, toLower; + import std.string : strip; url = url.strip(); _raw = url; @@ -46,7 +64,7 @@ static if (isWeb) { import diamond.core : webConfig; - _name = webConfig.homeRoute.toLower(); + _name = webConfig.homeRoute.firstToLower(); return; } @@ -67,7 +85,7 @@ static if (isWeb) enforce(!routeData[$-1].canFind("?"), "Found query string in the routing url."); - _name = routeData[0].strip().toLower(); + _name = routeData[0].strip().firstToLower(); if (routeData.length > 1) {