Skip to content

Commit

Permalink
Added response to authentication
Browse files Browse the repository at this point in the history
And also fixed routes only making first character to lower.
  • Loading branch information
bausshf committed Oct 21, 2017
1 parent 1444a9e commit cf21ff8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 17 deletions.
17 changes: 13 additions & 4 deletions src/controllers/authentication.d
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;

Expand All @@ -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;
}
Expand All @@ -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; }

Expand All @@ -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.
Expand Down
36 changes: 26 additions & 10 deletions src/controllers/controller.d
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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))
Expand Down Expand Up @@ -48,7 +66,7 @@ static if (isWeb)
(
action_%s.action && action_%s.action.strip().length ?
action_%s.action : "%s"
).toLower(),
).firstToLower(),
&controller.%s
);
}
Expand All @@ -68,7 +86,7 @@ static if (isWeb)
(
action_%s.action && action_%s.action.strip().length ?
action_%s.action : "%s"
).toLower()
).firstToLower()
);
}
}
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down
24 changes: 21 additions & 3 deletions src/http/route.d
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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;
Expand All @@ -46,7 +64,7 @@ static if (isWeb)
{
import diamond.core : webConfig;

_name = webConfig.homeRoute.toLower();
_name = webConfig.homeRoute.firstToLower();
return;
}

Expand All @@ -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)
{
Expand Down

0 comments on commit cf21ff8

Please sign in to comment.