Exemple #1
0
        public APIModule() : base("/api")
        {
            StatelessAuthentication.Enable(this, AobaCore.StatelessConfig);
            Before.AddItemToEndOfPipeline(ctx =>
            {
                return((this.Context.CurrentUser == null) ? new HtmlResponse(HttpStatusCode.Unauthorized) : null);
            });

            Get("/regToken", p =>
            {
                var user = Context.CurrentUser as UserModel;
                return(AobaCore.GetNewRegToken(user.ID));
            });

            Get("/userStats", _ =>
            {
                var uid = ((UserModel)Context.CurrentUser).ID;
                return(Response.AsJson(AobaCore.GetUserStats(uid)).WithHeader("Authorization", $"Bearer {AobaCore.GetJWT(AobaCore.GetApiKey(uid), 365)}"));
            });

            Get("/", _ =>
            {
                return(new Response {
                    StatusCode = HttpStatusCode.OK
                });
            });

            Post("/image", p =>
            {
                try
                {
                    var f     = Context.Request.Files.First();
                    var media = new MediaModel
                    {
                        type        = MediaModel.GetMediaType(f.Name),
                        mediaStream = f.Value,
                        fileName    = f.Name
                    };

                    media.mediaStream.Position = 0;
                    if (string.IsNullOrEmpty(media.Ext))
                    {
                        var ext        = GetImageExt(media.mediaStream);
                        media.fileName = $"{media.fileName}{ext}";
                        media.type     = MediaModel.GetMediaType(ext);
                    }
                    var uid = ((UserModel)Context.CurrentUser).ID;
                    AobaCore.AddMedia(uid, media);
                    var response = string.Empty;
                    if (media.type == MediaModel.MediaType.Raw)
                    {
                        response = $"{AobaCore.HOST}/i/raw/{media.id}/{media.fileName}";
                    }
                    else if (media.Ext == ".gif")
                    {
                        response = $"{AobaCore.HOST}/i/raw/{media.id}/{media.fileName}";
                    }
                    else
                    {
                        response = $"{AobaCore.HOST}/i/{media.id}";
                    }
                    if (Context.Request.Headers.AcceptEncoding.Contains("JSON"))
                    {
                        return(Response.AsJson(new
                        {
                            id = media.id,
                            url = response
                        }));
                    }
                    return(Response.AsText(response).WithHeader("Authorization", $"Bearer {AobaCore.GetJWT(AobaCore.GetApiKey(uid), 365)}"));
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.StackTrace);
                    return(new Response()
                    {
                        StatusCode = HttpStatusCode.ImATeapot
                    });
                }
            });

            Delete("image/{id}", p =>
            {
                AobaCore.DeleteImage(p.id);
                return(new Response()
                {
                    StatusCode = HttpStatusCode.OK
                });
            });
        }
Exemple #2
0
        public AuthModule() : base("/auth")
        {
            Post("/login", p =>
            {
                LoginCredentialsModel user = this.Bind <LoginCredentialsModel>();
                if (user == null)
                {
                    return new Response {
                        StatusCode = HttpStatusCode.Unauthorized
                    }
                }
                ;
                string apiKey = AobaCore.ValidateUser(user);
                if (apiKey == null)
                {
                    return new Response {
                        StatusCode = HttpStatusCode.Unauthorized
                    }
                }
                ;
                if (user.AuthMode == AuthMode.API)
                {
                    return(Response.AsJson(new { jwt = AobaCore.GetJWT(apiKey, 365) }));
                }
                else
                {
                    var token = AobaCore.GetJWT(apiKey);
                    return(new Response().WithHeader("Authorization", $"Bearer {token}").WithCookie("token", token));
                }
            });

            Get("/logout", _ =>
            {
                return(new Response().WithCookie("token", ""));
            });

            Post("/register/{token}", p =>
            {
                LoginCredentialsModel user = this.Bind <LoginCredentialsModel>();
                var token = (string)p.token;
                if (!string.IsNullOrWhiteSpace(token) && AobaCore.RegisterUser(user, token.Replace(' ', '+')))
                {
                    return(new Response {
                        StatusCode = HttpStatusCode.OK
                    });
                }
                else
                {
                    return new Response {
                        StatusCode = HttpStatusCode.Unauthorized
                    }
                };
            });

            Post("/checkuser", p =>
            {
                return((AobaCore.UserExists(Request.Body.AsString())) ? new Response {
                    StatusCode = HttpStatusCode.NotAcceptable
                } : new Response {
                    StatusCode = HttpStatusCode.OK
                });
            });
        }