コード例 #1
0
        public RootModule(IErrorService error)
        {
            Get["/"] = _ =>
            {
                return("ThumbsUp Security Service is Running");
            };

            Get["/error/{code}"] = url =>
            {
                if (Params.AreMissing("Code"))
                {
                    return(error.MissingParameters(Response));
                }
                return(Response.AsJson(new { ErrorCode = Params.Code, ErrorMessage = error.Decode(Params.Code) }));
            };
        }
コード例 #2
0
        public ApplicationModule(IApplicationService applicationService, IErrorService error)
            : base("/application")
        {
            Post["/register"] = _ =>
            {
                if (Params.AreMissing("SingleUseToken", "Name"))
                {
                    return(error.MissingParameters(Response));
                }
                if (!Params.SingleUseToken.IsGuid())
                {
                    return(error.InvalidParameters(Response));
                }
                if (!applicationService.AuthoriseSingleUseToken(Params.SingleUseToken))
                {
                    return(error.PermissionDenied(Response));
                }

                var application = applicationService.Register(Params.Name);
                return((application == null) ? error.InvalidParameters(Response) : Response.AsJson(new { ApplicationId = application.Id }));
            };

            Post["/transfer"] = _ =>
            {
                if (Params.AreMissing("SingleUseToken", "Name", "Id"))
                {
                    return(error.MissingParameters(Response));
                }
                if (!Params.Id.IsGuid() || !Params.SingleUseToken.IsGuid())
                {
                    return(error.InvalidParameters(Response));
                }
                if (!applicationService.AuthoriseSingleUseToken(Params.SingleUseToken))
                {
                    return(error.PermissionDenied(Response));
                }

                var application = applicationService.Transfer(Params.Name, Params.Id);
                return((application == null) ? error.InvalidParameters(Response) : Response.AsJson(new { ApplicationId = application.Id }));
            };
        }
コード例 #3
0
        public UserModule(IUserService userService, IErrorService error, IPasswordService passwordService, IUserCacheService userCacheService) : base("/user")
        {
            Post["/create"] = _ =>
            {
                if (Params.AreMissing("UserName", "Email"))
                {
                    return(error.MissingParameters(Response));
                }
                if (userService.GetUserByName(Params.UserName) != null)
                {
                    return(error.UserNameTaken(Response));
                }
                if (!Params.Email.IsEmail())
                {
                    return(error.InvalidParameters(Response));
                }
                var password = userService.CreateUser(Params.UserName, Params.Email);
                return((string.IsNullOrWhiteSpace(password)) ? error.InvalidParameters(Response) : Response.AsJson(new { Password = password }));
            };

            Post["/validate"] = _ =>
            {
                if (Params.AreMissing("UserName", "Password"))
                {
                    return(error.MissingParameters(Response));
                }
                var user = userService.GetUserByName(Params.UserName);
                if (user == null || !passwordService.IsPasswordValid(user, Params.Password))
                {
                    return(error.NoUserForCredentials(Response));
                }
                var key = userCacheService.Add(user);
                return((key == null) ? error.InvalidParameters(Response) : Response.AsJson(new { ThumbKey = key }));
            };

            Post["/get"] = _ =>
            {
                if (Params.AreMissing("ThumbKey"))
                {
                    return(error.MissingParameters(Response));
                }
                if (!Params.ThumbKey.IsGuid())
                {
                    return(error.InvalidParameters(Response));
                }
                var user = userCacheService.GetUser(Params.ThumbKey);
                return((user == null) ? error.NoUserForThumbkey(Response) : Response.AsJson(new { User = new { Id = user.Id, UserName = user.UserName, Email = user.Email } }));
            };

            Post["/validate/thumbkey"] = _ =>
            {
                if (Params.AreMissing("ThumbKey"))
                {
                    return(error.MissingParameters(Response));
                }
                if (!Params.ThumbKey.IsGuid())
                {
                    return(error.InvalidParameters(Response));
                }
                var isValid = userCacheService.Validate(Params.ThumbKey);
                return(!isValid?error.NoUserForThumbkey(Response) : HttpStatusCode.OK);
            };

            Post["/validate/name"] = _ =>
            {
                if (Params.AreMissing("UserName"))
                {
                    return(error.MissingParameters(Response));
                }
                var isValid = userService.GetUserByName(Params.UserName) == null;
                return(!isValid?error.UserNameTaken(Response) : HttpStatusCode.OK);
            };

            Post["/reset/password"] = _ =>
            {
                if (Params.AreMissing("UserName", "Password"))
                {
                    return(error.MissingParameters(Response));
                }
                var user = userService.GetUserByName(Params.UserName);
                if (user == null || !passwordService.IsPasswordValid(user, Params.Password))
                {
                    return(error.NoUserForCredentials(Response));
                }
                var password = userService.ResetPassword(user);
                return((password == null) ? error.InvalidParameters(Response) : Response.AsJson(new { Password = password }));
            };

            Post["/forgot-password/request"] = _ =>
            {
                if (Params.AreMissing("UserName", "Email"))
                {
                    return(error.MissingParameters(Response));
                }
                if (!Params.Email.IsEmail())
                {
                    return(error.InvalidParameters(Response));
                }
                var user = userService.GetUserByName(Params.UserName);
                if (user == null)
                {
                    return(error.NoUserForCredentials(Response));
                }
                if (user.Email != Params.Email)
                {
                    return(error.NoUserForEmail(Response));
                }
                var token = userService.ForgotPasswordRequest(user);
                return((token == null) ? error.InvalidParameters(Response) : Response.AsJson(new { Token = token }));
            };

            Post["/forgot-password/reset"] = _ =>
            {
                if (Params.AreMissing("UserName", "Token"))
                {
                    return(error.MissingParameters(Response));
                }
                if (!Params.Token.IsGuid())
                {
                    return(error.InvalidParameters(Response));
                }
                var user = userService.GetUserByName(Params.UserName);
                if (user == null || !passwordService.IsForgotPasswordTokenValid(user, Params.Token))
                {
                    return(error.InvalidForgotPasswordToken(Response));
                }
                var password = userService.ForgotPasswordReset(user);
                return((password == null) ? error.InvalidParameters(Response) : Response.AsJson(new { Password = password }));
            };

            Post["/logout"] = _ =>
            {
                if (Params.AreMissing("ThumbKey"))
                {
                    return(error.MissingParameters(Response));
                }
                if (!Params.ThumbKey.IsGuid())
                {
                    return(error.InvalidParameters(Response));
                }
                var success = userCacheService.Remove(Params.ThumbKey);
                return(!success?error.NoUserForThumbkey(Response) : HttpStatusCode.OK);
            };
        }