more account settings page improvements, among others
This commit is contained in:
parent
39b332b477
commit
686eea09a5
|
@ -3,13 +3,24 @@ package admin
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
"time"
|
||||||
|
|
||||||
"arimelody-web/controller"
|
"arimelody-web/controller"
|
||||||
|
"arimelody-web/global"
|
||||||
"arimelody-web/model"
|
"arimelody-web/model"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
|
"golang.org/x/crypto/bcrypt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type TemplateData struct {
|
||||||
|
Account *model.Account
|
||||||
|
Message string
|
||||||
|
Token string
|
||||||
|
}
|
||||||
|
|
||||||
func AccountHandler(db *sqlx.DB) http.Handler {
|
func AccountHandler(db *sqlx.DB) http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
account := r.Context().Value("account").(*model.Account)
|
account := r.Context().Value("account").(*model.Account)
|
||||||
|
@ -36,3 +47,299 @@ func AccountHandler(db *sqlx.DB) http.Handler {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func LoginHandler(db *sqlx.DB) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method == http.MethodGet {
|
||||||
|
account, err := controller.GetAccountByRequest(db, r)
|
||||||
|
if err != nil {
|
||||||
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
|
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch account: %v\n", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if account != nil {
|
||||||
|
http.Redirect(w, r, "/admin", http.StatusFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = pages["login"].Execute(w, TemplateData{})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "WARN: Error rendering admin login page: %s\n", err)
|
||||||
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type LoginResponse struct {
|
||||||
|
Account *model.Account
|
||||||
|
Token string
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
|
render := func(data LoginResponse) {
|
||||||
|
err := pages["login"].Execute(w, data)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "WARN: Error rendering admin login page: %s\n", err)
|
||||||
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.NotFound(w, r);
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err := r.ParseForm()
|
||||||
|
if err != nil {
|
||||||
|
render(LoginResponse{ Message: "Malformed request." })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type LoginRequest struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
TOTP string `json:"totp"`
|
||||||
|
}
|
||||||
|
credentials := LoginRequest{
|
||||||
|
Username: r.Form.Get("username"),
|
||||||
|
Password: r.Form.Get("password"),
|
||||||
|
TOTP: r.Form.Get("totp"),
|
||||||
|
}
|
||||||
|
|
||||||
|
account, err := controller.GetAccount(db, credentials.Username)
|
||||||
|
if err != nil {
|
||||||
|
render(LoginResponse{ Message: "Invalid username or password" })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if account == nil {
|
||||||
|
render(LoginResponse{ Message: "Invalid username or password" })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = bcrypt.CompareHashAndPassword([]byte(account.Password), []byte(credentials.Password))
|
||||||
|
if err != nil {
|
||||||
|
render(LoginResponse{ Message: "Invalid username or password" })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
totps, err := controller.GetTOTPsForAccount(db, account.ID)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch TOTPs: %v\n", err)
|
||||||
|
render(LoginResponse{ Message: "Something went wrong. Please try again." })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if len(totps) > 0 {
|
||||||
|
success := false
|
||||||
|
for _, totp := range totps {
|
||||||
|
check := controller.GenerateTOTP(totp.Secret, 0)
|
||||||
|
if check == credentials.TOTP {
|
||||||
|
success = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !success {
|
||||||
|
render(LoginResponse{ Message: "Invalid TOTP" })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// TODO: user should be prompted to add 2FA method
|
||||||
|
}
|
||||||
|
|
||||||
|
// login success!
|
||||||
|
token, err := controller.CreateToken(db, account.ID, r.UserAgent())
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "WARN: Failed to create token: %v\n", err)
|
||||||
|
render(LoginResponse{ Message: "Something went wrong. Please try again." })
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cookie := http.Cookie{}
|
||||||
|
cookie.Name = global.COOKIE_TOKEN
|
||||||
|
cookie.Value = token.Token
|
||||||
|
cookie.Expires = token.ExpiresAt
|
||||||
|
if strings.HasPrefix(global.Config.BaseUrl, "https") {
|
||||||
|
cookie.Secure = true
|
||||||
|
}
|
||||||
|
cookie.HttpOnly = true
|
||||||
|
cookie.Path = "/"
|
||||||
|
http.SetCookie(w, &cookie)
|
||||||
|
|
||||||
|
render(LoginResponse{ Account: account, Token: token.Token })
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func LogoutHandler(db *sqlx.DB) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodGet {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
tokenStr := controller.GetTokenFromRequest(db, r)
|
||||||
|
|
||||||
|
if len(tokenStr) > 0 {
|
||||||
|
err := controller.DeleteToken(db, tokenStr)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "WARN: Failed to revoke token: %v\n", err)
|
||||||
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
cookie := http.Cookie{}
|
||||||
|
cookie.Name = global.COOKIE_TOKEN
|
||||||
|
cookie.Value = ""
|
||||||
|
cookie.Expires = time.Now()
|
||||||
|
if strings.HasPrefix(global.Config.BaseUrl, "https") {
|
||||||
|
cookie.Secure = true
|
||||||
|
}
|
||||||
|
cookie.HttpOnly = true
|
||||||
|
cookie.Path = "/"
|
||||||
|
http.SetCookie(w, &cookie)
|
||||||
|
http.Redirect(w, r, "/admin/login", http.StatusFound)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func createAccountHandler(db *sqlx.DB) http.Handler {
|
||||||
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
checkAccount, err := controller.GetAccountByRequest(db, r)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("WARN: Failed to fetch account: %s\n", err)
|
||||||
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if checkAccount != nil {
|
||||||
|
// user is already logged in
|
||||||
|
http.Redirect(w, r, "/admin", http.StatusFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type CreateAccountResponse struct {
|
||||||
|
Account *model.Account
|
||||||
|
Message string
|
||||||
|
}
|
||||||
|
|
||||||
|
render := func(data CreateAccountResponse) {
|
||||||
|
err := pages["create-account"].Execute(w, data)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Printf("WARN: Error rendering create account page: %s\n", err)
|
||||||
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.Method == http.MethodGet {
|
||||||
|
render(CreateAccountResponse{})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if r.Method != http.MethodPost {
|
||||||
|
http.NotFound(w, r)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = r.ParseForm()
|
||||||
|
if err != nil {
|
||||||
|
render(CreateAccountResponse{
|
||||||
|
Message: "Malformed data.",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type RegisterRequest struct {
|
||||||
|
Username string `json:"username"`
|
||||||
|
Email string `json:"email"`
|
||||||
|
Password string `json:"password"`
|
||||||
|
Invite string `json:"invite"`
|
||||||
|
}
|
||||||
|
credentials := RegisterRequest{
|
||||||
|
Username: r.Form.Get("username"),
|
||||||
|
Email: r.Form.Get("email"),
|
||||||
|
Password: r.Form.Get("password"),
|
||||||
|
Invite: r.Form.Get("invite"),
|
||||||
|
}
|
||||||
|
|
||||||
|
// make sure code exists in DB
|
||||||
|
invite, err := controller.GetInvite(db, credentials.Invite)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "WARN: Failed to retrieve invite: %v\n", err)
|
||||||
|
render(CreateAccountResponse{
|
||||||
|
Message: "Something went wrong. Please try again.",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if invite == nil || time.Now().After(invite.ExpiresAt) {
|
||||||
|
if invite != nil {
|
||||||
|
err := controller.DeleteInvite(db, invite.Code)
|
||||||
|
if err != nil { fmt.Fprintf(os.Stderr, "WARN: Failed to delete expired invite: %v\n", err) }
|
||||||
|
}
|
||||||
|
render(CreateAccountResponse{
|
||||||
|
Message: "Invalid invite code.",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(credentials.Password), bcrypt.DefaultCost)
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "WARN: Failed to generate password hash: %v\n", err)
|
||||||
|
render(CreateAccountResponse{
|
||||||
|
Message: "Something went wrong. Please try again.",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
account := model.Account{
|
||||||
|
Username: credentials.Username,
|
||||||
|
Password: string(hashedPassword),
|
||||||
|
Email: credentials.Email,
|
||||||
|
AvatarURL: "/img/default-avatar.png",
|
||||||
|
}
|
||||||
|
err = controller.CreateAccount(db, &account)
|
||||||
|
if err != nil {
|
||||||
|
if strings.HasPrefix(err.Error(), "pq: duplicate key") {
|
||||||
|
render(CreateAccountResponse{
|
||||||
|
Message: "An account with that username already exists.",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
fmt.Fprintf(os.Stderr, "WARN: Failed to create account: %v\n", err)
|
||||||
|
render(CreateAccountResponse{
|
||||||
|
Message: "Something went wrong. Please try again.",
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
err = controller.DeleteInvite(db, invite.Code)
|
||||||
|
if err != nil { fmt.Fprintf(os.Stderr, "WARN: Failed to delete expired invite: %v\n", err) }
|
||||||
|
|
||||||
|
// registration success!
|
||||||
|
token, err := controller.CreateToken(db, account.ID, r.UserAgent())
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "WARN: Failed to create token: %v\n", err)
|
||||||
|
// gracefully redirect user to login page
|
||||||
|
http.Redirect(w, r, "/admin/login", http.StatusFound)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
cookie := http.Cookie{}
|
||||||
|
cookie.Name = global.COOKIE_TOKEN
|
||||||
|
cookie.Value = token.Token
|
||||||
|
cookie.Expires = token.ExpiresAt
|
||||||
|
if strings.HasPrefix(global.Config.BaseUrl, "https") {
|
||||||
|
cookie.Secure = true
|
||||||
|
}
|
||||||
|
cookie.HttpOnly = true
|
||||||
|
cookie.Path = "/"
|
||||||
|
http.SetCookie(w, &cookie)
|
||||||
|
|
||||||
|
err = pages["login"].Execute(w, TemplateData{
|
||||||
|
Account: &account,
|
||||||
|
Token: token.Token,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
fmt.Fprintf(os.Stderr, "WARN: Failed to render login page: %v\n", err)
|
||||||
|
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
278
admin/http.go
278
admin/http.go
|
@ -6,22 +6,13 @@ import (
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"arimelody-web/controller"
|
"arimelody-web/controller"
|
||||||
"arimelody-web/global"
|
|
||||||
"arimelody-web/model"
|
"arimelody-web/model"
|
||||||
|
|
||||||
"github.com/jmoiron/sqlx"
|
"github.com/jmoiron/sqlx"
|
||||||
"golang.org/x/crypto/bcrypt"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
type TemplateData struct {
|
|
||||||
Account *model.Account
|
|
||||||
Token string
|
|
||||||
}
|
|
||||||
|
|
||||||
func Handler(db *sqlx.DB) http.Handler {
|
func Handler(db *sqlx.DB) http.Handler {
|
||||||
mux := http.NewServeMux()
|
mux := http.NewServeMux()
|
||||||
|
|
||||||
|
@ -112,275 +103,6 @@ func RequireAccount(db *sqlx.DB, next http.Handler) http.HandlerFunc {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func LoginHandler(db *sqlx.DB) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.Method == http.MethodGet {
|
|
||||||
account, err := controller.GetAccountByRequest(db, r)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to fetch account: %v\n", err)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if account != nil {
|
|
||||||
http.Redirect(w, r, "/admin", http.StatusFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = pages["login"].Execute(w, TemplateData{})
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Error rendering admin login page: %s\n", err)
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.Method != http.MethodPost {
|
|
||||||
http.NotFound(w, r);
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err := r.ParseForm()
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
type LoginRequest struct {
|
|
||||||
Username string `json:"username"`
|
|
||||||
Password string `json:"password"`
|
|
||||||
TOTP string `json:"totp"`
|
|
||||||
}
|
|
||||||
credentials := LoginRequest{
|
|
||||||
Username: r.Form.Get("username"),
|
|
||||||
Password: r.Form.Get("password"),
|
|
||||||
TOTP: r.Form.Get("totp"),
|
|
||||||
}
|
|
||||||
|
|
||||||
account, err := controller.GetAccount(db, credentials.Username)
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Invalid username or password", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if account == nil {
|
|
||||||
http.Error(w, "Invalid username or password", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = bcrypt.CompareHashAndPassword([]byte(account.Password), []byte(credentials.Password))
|
|
||||||
if err != nil {
|
|
||||||
http.Error(w, "Invalid username or password", http.StatusBadRequest)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// TODO: check TOTP
|
|
||||||
|
|
||||||
// login success!
|
|
||||||
token, err := controller.CreateToken(db, account.ID, r.UserAgent())
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to create token: %v\n", err)
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cookie := http.Cookie{}
|
|
||||||
cookie.Name = global.COOKIE_TOKEN
|
|
||||||
cookie.Value = token.Token
|
|
||||||
cookie.Expires = token.ExpiresAt
|
|
||||||
if strings.HasPrefix(global.Config.BaseUrl, "https") {
|
|
||||||
cookie.Secure = true
|
|
||||||
}
|
|
||||||
cookie.HttpOnly = true
|
|
||||||
cookie.Path = "/"
|
|
||||||
http.SetCookie(w, &cookie)
|
|
||||||
|
|
||||||
err = pages["login"].Execute(w, TemplateData{
|
|
||||||
Account: account,
|
|
||||||
Token: token.Token,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("Error rendering admin login page: %s\n", err)
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func LogoutHandler(db *sqlx.DB) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
if r.Method != http.MethodGet {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
tokenStr := controller.GetTokenFromRequest(db, r)
|
|
||||||
|
|
||||||
if len(tokenStr) > 0 {
|
|
||||||
err := controller.DeleteToken(db, tokenStr)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to revoke token: %v\n", err)
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
cookie := http.Cookie{}
|
|
||||||
cookie.Name = global.COOKIE_TOKEN
|
|
||||||
cookie.Value = ""
|
|
||||||
cookie.Expires = time.Now()
|
|
||||||
if strings.HasPrefix(global.Config.BaseUrl, "https") {
|
|
||||||
cookie.Secure = true
|
|
||||||
}
|
|
||||||
cookie.HttpOnly = true
|
|
||||||
cookie.Path = "/"
|
|
||||||
http.SetCookie(w, &cookie)
|
|
||||||
http.Redirect(w, r, "/admin/login", http.StatusFound)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func createAccountHandler(db *sqlx.DB) http.Handler {
|
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
||||||
checkAccount, err := controller.GetAccountByRequest(db, r)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("WARN: Failed to fetch account: %s\n", err)
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if checkAccount != nil {
|
|
||||||
// user is already logged in
|
|
||||||
http.Redirect(w, r, "/admin", http.StatusFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
type CreateAccountResponse struct {
|
|
||||||
Account *model.Account
|
|
||||||
Message string
|
|
||||||
}
|
|
||||||
|
|
||||||
render := func(data CreateAccountResponse) {
|
|
||||||
err := pages["create-account"].Execute(w, data)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Printf("WARN: Error rendering create account page: %s\n", err)
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.Method == http.MethodGet {
|
|
||||||
render(CreateAccountResponse{})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if r.Method != http.MethodPost {
|
|
||||||
http.NotFound(w, r)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = r.ParseForm()
|
|
||||||
if err != nil {
|
|
||||||
render(CreateAccountResponse{
|
|
||||||
Message: "Malformed data.",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
type RegisterRequest struct {
|
|
||||||
Username string `json:"username"`
|
|
||||||
Email string `json:"email"`
|
|
||||||
Password string `json:"password"`
|
|
||||||
Invite string `json:"invite"`
|
|
||||||
}
|
|
||||||
credentials := RegisterRequest{
|
|
||||||
Username: r.Form.Get("username"),
|
|
||||||
Email: r.Form.Get("email"),
|
|
||||||
Password: r.Form.Get("password"),
|
|
||||||
Invite: r.Form.Get("invite"),
|
|
||||||
}
|
|
||||||
|
|
||||||
// make sure code exists in DB
|
|
||||||
invite, err := controller.GetInvite(db, credentials.Invite)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to retrieve invite: %v\n", err)
|
|
||||||
render(CreateAccountResponse{
|
|
||||||
Message: "Something went wrong. Please try again.",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
if invite == nil || time.Now().After(invite.ExpiresAt) {
|
|
||||||
if invite != nil {
|
|
||||||
err := controller.DeleteInvite(db, invite.Code)
|
|
||||||
if err != nil { fmt.Fprintf(os.Stderr, "WARN: Failed to delete expired invite: %v\n", err) }
|
|
||||||
}
|
|
||||||
render(CreateAccountResponse{
|
|
||||||
Message: "Invalid invite code.",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(credentials.Password), bcrypt.DefaultCost)
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to generate password hash: %v\n", err)
|
|
||||||
render(CreateAccountResponse{
|
|
||||||
Message: "Something went wrong. Please try again.",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
account := model.Account{
|
|
||||||
Username: credentials.Username,
|
|
||||||
Password: string(hashedPassword),
|
|
||||||
Email: credentials.Email,
|
|
||||||
AvatarURL: "/img/default-avatar.png",
|
|
||||||
}
|
|
||||||
err = controller.CreateAccount(db, &account)
|
|
||||||
if err != nil {
|
|
||||||
if strings.HasPrefix(err.Error(), "pq: duplicate key") {
|
|
||||||
render(CreateAccountResponse{
|
|
||||||
Message: "An account with that username already exists.",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to create account: %v\n", err)
|
|
||||||
render(CreateAccountResponse{
|
|
||||||
Message: "Something went wrong. Please try again.",
|
|
||||||
})
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
err = controller.DeleteInvite(db, invite.Code)
|
|
||||||
if err != nil { fmt.Fprintf(os.Stderr, "WARN: Failed to delete expired invite: %v\n", err) }
|
|
||||||
|
|
||||||
// registration success!
|
|
||||||
token, err := controller.CreateToken(db, account.ID, r.UserAgent())
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to create token: %v\n", err)
|
|
||||||
// gracefully redirect user to login page
|
|
||||||
http.Redirect(w, r, "/admin/login", http.StatusFound)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cookie := http.Cookie{}
|
|
||||||
cookie.Name = global.COOKIE_TOKEN
|
|
||||||
cookie.Value = token.Token
|
|
||||||
cookie.Expires = token.ExpiresAt
|
|
||||||
if strings.HasPrefix(global.Config.BaseUrl, "https") {
|
|
||||||
cookie.Secure = true
|
|
||||||
}
|
|
||||||
cookie.HttpOnly = true
|
|
||||||
cookie.Path = "/"
|
|
||||||
http.SetCookie(w, &cookie)
|
|
||||||
|
|
||||||
err = pages["login"].Execute(w, TemplateData{
|
|
||||||
Account: &account,
|
|
||||||
Token: token.Token,
|
|
||||||
})
|
|
||||||
if err != nil {
|
|
||||||
fmt.Fprintf(os.Stderr, "WARN: Failed to render login page: %v\n", err)
|
|
||||||
http.Error(w, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func staticHandler() http.Handler {
|
func staticHandler() http.Handler {
|
||||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
info, err := os.Stat(filepath.Join("admin", "static", filepath.Clean(r.URL.Path)))
|
info, err := os.Stat(filepath.Join("admin", "static", filepath.Clean(r.URL.Path)))
|
||||||
|
|
|
@ -124,3 +124,65 @@ a img.icon {
|
||||||
font-size: 12px;
|
font-size: 12px;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
#error {
|
||||||
|
background: #ffa9b8;
|
||||||
|
border: 1px solid #dc5959;
|
||||||
|
padding: 1em;
|
||||||
|
border-radius: 4px;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
button, .button {
|
||||||
|
padding: .5em .8em;
|
||||||
|
font-family: inherit;
|
||||||
|
font-size: inherit;
|
||||||
|
border-radius: .5em;
|
||||||
|
border: 1px solid #a0a0a0;
|
||||||
|
background: #f0f0f0;
|
||||||
|
color: inherit;
|
||||||
|
}
|
||||||
|
button:hover, .button:hover {
|
||||||
|
background: #fff;
|
||||||
|
border-color: #d0d0d0;
|
||||||
|
}
|
||||||
|
button:active, .button:active {
|
||||||
|
background: #d0d0d0;
|
||||||
|
border-color: #808080;
|
||||||
|
}
|
||||||
|
|
||||||
|
button {
|
||||||
|
color: inherit;
|
||||||
|
}
|
||||||
|
button.new {
|
||||||
|
background: #c4ff6a;
|
||||||
|
border-color: #84b141;
|
||||||
|
}
|
||||||
|
button.save {
|
||||||
|
background: #6fd7ff;
|
||||||
|
border-color: #6f9eb0;
|
||||||
|
}
|
||||||
|
button.delete {
|
||||||
|
background: #ff7171;
|
||||||
|
border-color: #7d3535;
|
||||||
|
}
|
||||||
|
button:hover {
|
||||||
|
background: #fff;
|
||||||
|
border-color: #d0d0d0;
|
||||||
|
}
|
||||||
|
button:active {
|
||||||
|
background: #d0d0d0;
|
||||||
|
border-color: #808080;
|
||||||
|
}
|
||||||
|
button[disabled] {
|
||||||
|
background: #d0d0d0 !important;
|
||||||
|
border-color: #808080 !important;
|
||||||
|
opacity: .5;
|
||||||
|
cursor: not-allowed !important;
|
||||||
|
}
|
||||||
|
a.delete {
|
||||||
|
color: #d22828;
|
||||||
|
}
|
||||||
|
|
|
@ -39,3 +39,27 @@ input {
|
||||||
padding: 1em;
|
padding: 1em;
|
||||||
border-radius: 4px;
|
border-radius: 4px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.mfa-device {
|
||||||
|
padding: .75em;
|
||||||
|
background: #f8f8f8f8;
|
||||||
|
border: 1px solid #808080;
|
||||||
|
border-radius: .5em;
|
||||||
|
margin-bottom: .5em;
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mfa-device div {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: column;
|
||||||
|
justify-content: center;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mfa-device p {
|
||||||
|
margin: 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.mfa-device .mfa-device-name {
|
||||||
|
font-weight: bold;
|
||||||
|
}
|
||||||
|
|
0
admin/static/edit-account.js
Normal file
0
admin/static/edit-account.js
Normal file
|
@ -66,54 +66,6 @@ input[type="text"]:focus {
|
||||||
border-color: #808080;
|
border-color: #808080;
|
||||||
}
|
}
|
||||||
|
|
||||||
button, .button {
|
|
||||||
padding: .5em .8em;
|
|
||||||
font-family: inherit;
|
|
||||||
font-size: inherit;
|
|
||||||
border-radius: .5em;
|
|
||||||
border: 1px solid #a0a0a0;
|
|
||||||
background: #f0f0f0;
|
|
||||||
color: inherit;
|
|
||||||
}
|
|
||||||
button:hover, .button:hover {
|
|
||||||
background: #fff;
|
|
||||||
border-color: #d0d0d0;
|
|
||||||
}
|
|
||||||
button:active, .button:active {
|
|
||||||
background: #d0d0d0;
|
|
||||||
border-color: #808080;
|
|
||||||
}
|
|
||||||
|
|
||||||
button {
|
|
||||||
color: inherit;
|
|
||||||
}
|
|
||||||
button.save {
|
|
||||||
background: #6fd7ff;
|
|
||||||
border-color: #6f9eb0;
|
|
||||||
}
|
|
||||||
button.delete {
|
|
||||||
background: #ff7171;
|
|
||||||
border-color: #7d3535;
|
|
||||||
}
|
|
||||||
button:hover {
|
|
||||||
background: #fff;
|
|
||||||
border-color: #d0d0d0;
|
|
||||||
}
|
|
||||||
button:active {
|
|
||||||
background: #d0d0d0;
|
|
||||||
border-color: #808080;
|
|
||||||
}
|
|
||||||
button[disabled] {
|
|
||||||
background: #d0d0d0 !important;
|
|
||||||
border-color: #808080 !important;
|
|
||||||
opacity: .5;
|
|
||||||
cursor: not-allowed !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
a.delete {
|
|
||||||
color: #d22828;
|
|
||||||
}
|
|
||||||
|
|
||||||
.artist-actions {
|
.artist-actions {
|
||||||
margin-top: auto;
|
margin-top: auto;
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|
|
@ -109,58 +109,6 @@ input[type="text"] {
|
||||||
padding: 0;
|
padding: 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
button, .button {
|
|
||||||
padding: .5em .8em;
|
|
||||||
font-family: inherit;
|
|
||||||
font-size: inherit;
|
|
||||||
border-radius: .5em;
|
|
||||||
border: 1px solid #a0a0a0;
|
|
||||||
background: #f0f0f0;
|
|
||||||
color: inherit;
|
|
||||||
}
|
|
||||||
button:hover, .button:hover {
|
|
||||||
background: #fff;
|
|
||||||
border-color: #d0d0d0;
|
|
||||||
}
|
|
||||||
button:active, .button:active {
|
|
||||||
background: #d0d0d0;
|
|
||||||
border-color: #808080;
|
|
||||||
}
|
|
||||||
|
|
||||||
button {
|
|
||||||
color: inherit;
|
|
||||||
}
|
|
||||||
button.new {
|
|
||||||
background: #c4ff6a;
|
|
||||||
border-color: #84b141;
|
|
||||||
}
|
|
||||||
button.save {
|
|
||||||
background: #6fd7ff;
|
|
||||||
border-color: #6f9eb0;
|
|
||||||
}
|
|
||||||
button.delete {
|
|
||||||
background: #ff7171;
|
|
||||||
border-color: #7d3535;
|
|
||||||
}
|
|
||||||
button:hover {
|
|
||||||
background: #fff;
|
|
||||||
border-color: #d0d0d0;
|
|
||||||
}
|
|
||||||
button:active {
|
|
||||||
background: #d0d0d0;
|
|
||||||
border-color: #808080;
|
|
||||||
}
|
|
||||||
button[disabled] {
|
|
||||||
background: #d0d0d0 !important;
|
|
||||||
border-color: #808080 !important;
|
|
||||||
opacity: .5;
|
|
||||||
cursor: not-allowed !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
a.delete {
|
|
||||||
color: #d22828;
|
|
||||||
}
|
|
||||||
|
|
||||||
.release-actions {
|
.release-actions {
|
||||||
margin-top: auto;
|
margin-top: auto;
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|
|
@ -67,54 +67,6 @@ h1 {
|
||||||
border-color: #808080;
|
border-color: #808080;
|
||||||
}
|
}
|
||||||
|
|
||||||
button, .button {
|
|
||||||
padding: .5em .8em;
|
|
||||||
font-family: inherit;
|
|
||||||
font-size: inherit;
|
|
||||||
border-radius: .5em;
|
|
||||||
border: 1px solid #a0a0a0;
|
|
||||||
background: #f0f0f0;
|
|
||||||
color: inherit;
|
|
||||||
}
|
|
||||||
button:hover, .button:hover {
|
|
||||||
background: #fff;
|
|
||||||
border-color: #d0d0d0;
|
|
||||||
}
|
|
||||||
button:active, .button:active {
|
|
||||||
background: #d0d0d0;
|
|
||||||
border-color: #808080;
|
|
||||||
}
|
|
||||||
|
|
||||||
button {
|
|
||||||
color: inherit;
|
|
||||||
}
|
|
||||||
button.save {
|
|
||||||
background: #6fd7ff;
|
|
||||||
border-color: #6f9eb0;
|
|
||||||
}
|
|
||||||
button.delete {
|
|
||||||
background: #ff7171;
|
|
||||||
border-color: #7d3535;
|
|
||||||
}
|
|
||||||
button:hover {
|
|
||||||
background: #fff;
|
|
||||||
border-color: #d0d0d0;
|
|
||||||
}
|
|
||||||
button:active {
|
|
||||||
background: #d0d0d0;
|
|
||||||
border-color: #808080;
|
|
||||||
}
|
|
||||||
button[disabled] {
|
|
||||||
background: #d0d0d0 !important;
|
|
||||||
border-color: #808080 !important;
|
|
||||||
opacity: .5;
|
|
||||||
cursor: not-allowed !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
a.delete {
|
|
||||||
color: #d22828;
|
|
||||||
}
|
|
||||||
|
|
||||||
.track-actions {
|
.track-actions {
|
||||||
margin-top: 1em;
|
margin-top: 1em;
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|
|
@ -98,49 +98,3 @@
|
||||||
.track .empty {
|
.track .empty {
|
||||||
opacity: 0.75;
|
opacity: 0.75;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
button, .button {
|
|
||||||
padding: .5em .8em;
|
|
||||||
font-family: inherit;
|
|
||||||
font-size: inherit;
|
|
||||||
border-radius: .5em;
|
|
||||||
border: 1px solid #a0a0a0;
|
|
||||||
background: #f0f0f0;
|
|
||||||
color: inherit;
|
|
||||||
}
|
|
||||||
button:hover, .button:hover {
|
|
||||||
background: #fff;
|
|
||||||
border-color: #d0d0d0;
|
|
||||||
}
|
|
||||||
button:active, .button:active {
|
|
||||||
background: #d0d0d0;
|
|
||||||
border-color: #808080;
|
|
||||||
}
|
|
||||||
|
|
||||||
button {
|
|
||||||
color: inherit;
|
|
||||||
}
|
|
||||||
button.save {
|
|
||||||
background: #6fd7ff;
|
|
||||||
border-color: #6f9eb0;
|
|
||||||
}
|
|
||||||
button.delete {
|
|
||||||
background: #ff7171;
|
|
||||||
border-color: #7d3535;
|
|
||||||
}
|
|
||||||
button:hover {
|
|
||||||
background: #fff;
|
|
||||||
border-color: #d0d0d0;
|
|
||||||
}
|
|
||||||
button:active {
|
|
||||||
background: #d0d0d0;
|
|
||||||
border-color: #808080;
|
|
||||||
}
|
|
||||||
button[disabled] {
|
|
||||||
background: #d0d0d0 !important;
|
|
||||||
border-color: #808080 !important;
|
|
||||||
opacity: .5;
|
|
||||||
cursor: not-allowed !important;
|
|
||||||
}
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{{define "head"}}
|
{{define "head"}}
|
||||||
<title>Register - ari melody 💫</title>
|
<title>Register - ari melody 💫</title>
|
||||||
<link rel="shortcut icon" href="/img/favicon.png" type="image/x-icon">
|
<link rel="shortcut icon" href="/img/favicon.png" type="image/x-icon">
|
||||||
<link rel="stylesheet" href="/admin/static/index.css">
|
<link rel="stylesheet" href="/admin/static/admin.css">
|
||||||
<style>
|
<style>
|
||||||
p a {
|
p a {
|
||||||
color: #2a67c8;
|
color: #2a67c8;
|
||||||
|
@ -43,13 +43,6 @@ input {
|
||||||
font-family: inherit;
|
font-family: inherit;
|
||||||
color: inherit;
|
color: inherit;
|
||||||
}
|
}
|
||||||
|
|
||||||
#error {
|
|
||||||
background: #ffa9b8;
|
|
||||||
border: 1px solid #dc5959;
|
|
||||||
padding: 1em;
|
|
||||||
border-radius: 4px;
|
|
||||||
}
|
|
||||||
</style>
|
</style>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
|
|
|
@ -35,15 +35,20 @@
|
||||||
{{if .TOTPs}}
|
{{if .TOTPs}}
|
||||||
{{range .TOTPs}}
|
{{range .TOTPs}}
|
||||||
<div class="mfa-device">
|
<div class="mfa-device">
|
||||||
<h3 class="mfa-device-name">{{.Name}}</h3>
|
<div>
|
||||||
<p class="mfa-device-date">{{.CreatedAt}}</p>
|
<p class="mfa-device-name">{{.Name}}</p>
|
||||||
|
<p class="mfa-device-date">Added: {{.CreatedAt}}</p>
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
<a class="delete">Delete</a>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{{end}}
|
{{end}}
|
||||||
{{else}}
|
{{else}}
|
||||||
<p>You have no MFA devices.</p>
|
<p>You have no MFA devices.</p>
|
||||||
{{end}}
|
{{end}}
|
||||||
|
|
||||||
<a class="create-btn" id="add-mfa-device">Add MFA Device</a>
|
<button type="submit" class="new" id="add-mfa-device">Add MFA Device</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="card-title">
|
<div class="card-title">
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{{define "head"}}
|
{{define "head"}}
|
||||||
<title>Login - ari melody 💫</title>
|
<title>Login - ari melody 💫</title>
|
||||||
<link rel="shortcut icon" href="/img/favicon.png" type="image/x-icon">
|
<link rel="shortcut icon" href="/img/favicon.png" type="image/x-icon">
|
||||||
|
<link rel="stylesheet" href="/admin/static/admin.css">
|
||||||
<style>
|
<style>
|
||||||
p a {
|
p a {
|
||||||
color: #2a67c8;
|
color: #2a67c8;
|
||||||
|
@ -52,6 +52,10 @@ input[disabled] {
|
||||||
|
|
||||||
{{define "content"}}
|
{{define "content"}}
|
||||||
<main>
|
<main>
|
||||||
|
{{if .Message}}
|
||||||
|
<p id="error">{{.Message}}</p>
|
||||||
|
{{end}}
|
||||||
|
|
||||||
{{if .Token}}
|
{{if .Token}}
|
||||||
|
|
||||||
<meta http-equiv="refresh" content="0;url=/admin/" />
|
<meta http-equiv="refresh" content="0;url=/admin/" />
|
||||||
|
|
Loading…
Reference in a new issue