예제 #1
0
        public AuthModule(IAppDbContext dbCtx, IResourceLinker linker) : base(dbCtx, linker)
        {
            var encoder = new SaltRawSaltPasswordEncoder();

            Get[RouteNames.GetLogin, "/auth/login"] = arg =>
            {
                AppModel.PageTitle = "Login";
                return(View["login", new { linker }]);
            };

            Get[RouteNames.GetLogout, "/auth/logout"] = arg =>
            {
                var rootPath = linker.BuildRelativeUri(Context, RouteNames.GetRoot).ToString();
                return(this.LogoutAndRedirect(rootPath));
            };

            Post[RouteNames.PostLogin, "/auth/login"] = parameters =>
            {
                var loginParams = this.Bind <AuthModel>();
                var member      = dbCtx.Users.FirstOrDefault(x => x.UserName == loginParams.Username);
                if (member == null || !encoder.IsPasswordValid(member.PassHash, loginParams.Password, member.PassSalt))
                {
                    return("username and/or password was incorrect");
                }
                var expiry   = loginParams.RememberMe ? DateTime.MaxValue : DateTime.Now.AddDays(14);
                var rootPath = linker.BuildRelativeUri(Context, RouteNames.GetRoot).ToString();
                return(this.LoginAndRedirect(member.Uuid, expiry, rootPath));
            };
        }
예제 #2
0
        public UserLoginModule(ISettingsService <AuthenticationSettings> auth, IPlexApi api, ISettingsService <PlexSettings> plexSettings, ISettingsService <PlexRequestSettings> pr,
                               ISettingsService <LandingPageSettings> lp, IAnalytics a, IResourceLinker linker) : base("userlogin", pr)
        {
            AuthService         = auth;
            LandingPageSettings = lp;
            Analytics           = a;
            Api          = api;
            PlexSettings = plexSettings;
            Linker       = linker;

            Get["UserLoginIndex", "/", true] = async(x, ct) =>
            {
                if (!string.IsNullOrEmpty(Username) || IsAdmin)
                {
                    var uri = Linker.BuildRelativeUri(Context, "SearchIndex");
                    return(Response.AsRedirect(uri.ToString()));
                }
                var settings = await AuthService.GetSettingsAsync();

                return(View["Index", settings]);
            };

            Post["/", true] = async(x, ct) => await LoginUser();

            Get["/logout"] = x => Logout();
        }
예제 #3
0
        public LandingPageModule(ISettingsService <PlexRequestSettings> settingsService, ISettingsService <LandingPageSettings> landing,
                                 ISettingsService <PlexSettings> ps, IPlexApi pApi, IResourceLinker linker, ISecurityExtensions security) : base("landing", settingsService, security)
        {
            LandingSettings = landing;
            PlexSettings    = ps;
            PlexApi         = pApi;
            Linker          = linker;

            Get["LandingPageIndex", "/", true] = async(x, ct) =>
            {
                var s = await LandingSettings.GetSettingsAsync();

                if (!s.BeforeLogin && string.IsNullOrEmpty(Username)) //We are signed in
                {
                    var url = Linker.BuildRelativeUri(Context, "SearchIndex").ToString();
                    return(Response.AsRedirect(url));
                }

                var model = new LandingPageViewModel
                {
                    Enabled           = s.Enabled,
                    Id                = s.Id,
                    EnabledNoticeTime = s.EnabledNoticeTime,
                    NoticeEnable      = s.NoticeEnable,
                    NoticeEnd         = s.NoticeEnd,
                    NoticeMessage     = s.NoticeMessage,
                    NoticeStart       = s.NoticeStart,
                    ContinueUrl       = s.BeforeLogin ? $"userlogin" : $"search"
                };
                return(View["Landing/Index", model]);
            };
            Get["/status", true] = async(x, ct) => await CheckStatus();
        }
예제 #4
0
        public RootModule(IAppDbContext context, IResourceLinker linker) : base(context, linker)
        {
            //Redirect the root to the home page route
            Get[RouteNames.GetRoot, "/"] = parameters =>
            {
                if (Context.CurrentUser != null)
                {
                    return(new RedirectResponse(
                               linker.BuildRelativeUri(Context, RouteNames.GetHome)
                               .ToString()));
                }

                //Otherwise, show the logged out version
                return(View["index", new { linker }]);
            };
        }
예제 #5
0
 public string BuildRelativeRoute(Route route, dynamic parameters = null)
 {
     return(_linker.BuildRelativeUri(_context, route.Name, parameters).ToString());
 }
예제 #6
0
        public LoginModule(ISettingsService <PlexRequestSettings> pr, ICustomUserMapper m, IResourceLinker linker, IRepository <UserLogins> userLoginRepo, ISecurityExtensions security)
            : base(pr, security)
        {
            UserMapper = m;
            Get["LocalLogin", "/login"] = _ =>
            {
                if (LoggedIn)
                {
                    var url = linker.BuildRelativeUri(Context, "SearchIndex");
                    return(Response.AsRedirect(url.ToString()));
                }
                dynamic model = new ExpandoObject();
                model.Redirect = Request.Query.redirect.Value ?? string.Empty;
                model.Errored  = Request.Query.error.HasValue;
                var adminCreated = UserMapper.DoUsersExist();
                model.AdminExists = adminCreated;
                return(View["Index", model]);
            };

            Get["/logout"] = x =>
            {
                if (Session[SessionKeys.UsernameKey] != null)
                {
                    Session.Delete(SessionKeys.UsernameKey);
                }
                return(CustomModuleExtensions.LogoutAndRedirect(this, !string.IsNullOrEmpty(BaseUrl) ? $"~/{BaseUrl}/" : "~/"));
            };

            Post["/login"] = x =>
            {
                var username = (string)Request.Form.Username;
                var password = (string)Request.Form.Password;
                var dtOffset = (int)Request.Form.DateTimeOffset;
                var redirect = (string)Request.Form.Redirect;

                var userId = UserMapper.ValidateUser(username, password);

                if (userId == null)
                {
                    return
                        (Context.GetRedirect(!string.IsNullOrEmpty(BaseUrl)
                            ? $"~/{BaseUrl}/login?error=true&username="******"~/login?error=true&username="******"userlogin"))
                {
                    redirect = !string.IsNullOrEmpty(BaseUrl) ? $"/{BaseUrl}/search" : "/search";
                }

                userLoginRepo.Insert(new UserLogins
                {
                    LastLoggedIn = DateTime.UtcNow,
                    Type         = UserType.LocalUser,
                    UserId       = userId.ToString()
                });

                return(CustomModuleExtensions.LoginAndRedirect(this, userId.Value, expiry, redirect));
            };

            Get["/register"] = x =>
            {
                {
                    dynamic model = new ExpandoObject();
                    model.Errored = Request.Query.error.HasValue;

                    return(View["Register", model]);
                }
            };

            Post["/register"] = x =>
            {
                var username = (string)Request.Form.Username;
                var exists   = UserMapper.DoUsersExist();
                if (exists)
                {
                    return
                        (Context.GetRedirect(!string.IsNullOrEmpty(BaseUrl)
                            ? $"~/{BaseUrl}/register?error=true"
                            : "~/register?error=true"));
                }
                var userId = UserMapper.CreateUser(username, Request.Form.Password, EnumHelper <Permissions> .All(), 0);
                Session[SessionKeys.UsernameKey] = username;
                return(CustomModuleExtensions.LoginAndRedirect(this, (Guid)userId));
            };

            Get["/changepassword"]  = _ => ChangePassword();
            Post["/changepassword"] = _ => ChangePasswordPost();
        }
예제 #7
0
        public UserLoginModule(ISettingsService <AuthenticationSettings> auth, IPlexApi api, ISettingsService <PlexSettings> plexSettings, ISettingsService <PlexRequestSettings> pr,
                               ISettingsService <LandingPageSettings> lp, IAnalytics a, IResourceLinker linker, IRepository <UserLogins> userLogins, IExternalUserRepository <PlexUsers> plexUsers, ICustomUserMapper custom,
                               ISecurityExtensions security, ISettingsService <UserManagementSettings> userManagementSettings, IEmbyApi embyApi, ISettingsService <EmbySettings> emby, IExternalUserRepository <EmbyUsers> embyU,
                               IUserHelper userHelper)
            : base("userlogin", pr, security)
        {
            AuthService         = auth;
            LandingPageSettings = lp;
            Analytics           = a;
            PlexApi             = api;
            PlexSettings        = plexSettings;
            Linker                 = linker;
            UserLogins             = userLogins;
            PlexUserRepository     = plexUsers;
            CustomUserMapper       = custom;
            UserManagementSettings = userManagementSettings;
            EmbySettings           = emby;
            EmbyApi                = embyApi;
            EmbyUserRepository     = embyU;
            UserHelper             = userHelper;

            Post["/", true] = async(x, ct) => await LoginUser();

            Get["/logout"] = x => Logout();

            Get["UserLoginIndex", "/", true] = async(x, ct) =>
            {
                if (Request.Query["landing"] == null)
                {
                    var s = await LandingPageSettings.GetSettingsAsync();

                    if (s.Enabled)
                    {
                        if (s.BeforeLogin) // Before login
                        {
                            if (string.IsNullOrEmpty(Username))
                            {
                                // They are not logged in
                                return
                                    (Context.GetRedirect(Linker.BuildRelativeUri(Context, "LandingPageIndex").ToString()));
                            }
                            return(Context.GetRedirect(Linker.BuildRelativeUri(Context, "SearchIndex").ToString()));
                        }

                        // After login
                        if (string.IsNullOrEmpty(Username))
                        {
                            // Not logged in yet
                            return(Context.GetRedirect(Linker.BuildRelativeUri(Context, "UserLoginIndex").ToString() + "?landing"));
                        }
                        // Send them to landing
                        var landingUrl = Linker.BuildRelativeUri(Context, "LandingPageIndex").ToString();
                        return(Context.GetRedirect(landingUrl));
                    }
                }

                if (!string.IsNullOrEmpty(Username) || IsAdmin)
                {
                    var url = Linker.BuildRelativeUri(Context, "SearchIndex").ToString();
                    return(Response.AsRedirect(url));
                }
                var settings = await AuthService.GetSettingsAsync();

                return(View["Username", settings]);
            };

            Post["/login", true] = async(x, ct) => await UsernameLogin();

            Post["/password", true] = async(x, ct) => await PasswordLogin();
        }