/// <summary>
        /// Creates the required routes for all IIdentityProvider implementations.
        /// </summary>
        /// <param name="identityProvider">The provider being extended</param>
        /// <param name="handler">A handler to wrap around the call. Takes the output and API input parameters as input.</param>
        /// <returns>The list of API routes</returns>
        public static List <APIRoute> GetRequiredAPIRoutes(this IIdentityProvider identityProvider, Func <dynamic, object[], dynamic> handler = null)
        {
            var routes = new List <APIRoute>();

            // For the IIdentityProvider interface we need to add routes to the API
            var route = new APIRoute
            {
                Path        = "/login",
                Description = "Log a user in",
                Method      = HttpMethod.Post,
                Handler     = (APIRequest request) =>
                {
                    // RequiredFields property is only implemented on the APIRouteAttribute
                    // so I have to manually do the checks for required interface routes
                    if (request.Data.username == null || request.Data.password == null)
                    {
                        throw new MissingFieldException("Username and password must not be null");
                    }

                    // Try..Catch as I'm calling user code here
                    var apiResponse = new APIResponse();

                    var loginResult = identityProvider.Login((string)request.Data.username, (string)request.Data.password);
                    if (loginResult.IsAuthenticated == false)
                    {
                        apiResponse.StatusCode    = (int)HttpStatusCode.Unauthorized;
                        apiResponse.StatusMessage = "Authentication required";
                        apiResponse.Data          = loginResult.ErrorResponse;
                    }
                    else
                    {
                        apiResponse.Data = loginResult.User;
                    }

                    if (handler == null)
                    {
                        return(apiResponse);
                    }

                    // TODO: the analysis module hook needs to be implemented in a better way
                    // Hiding the password
                    request.Data.password = "******";
                    return(handler(apiResponse, new object[] { request }));
                }
            };

            routes.Add(route);

            return(routes);
        }
Example #2
0
 public async Task <bool> Login(string userEmail, string password, bool isPersistent = false)
 {
     return(await _identityProvider.Login(userEmail, password, isPersistent));
 }