2020-07-05 01:02:57 +02:00
|
|
|
package route
|
2020-04-10 04:40:22 +02:00
|
|
|
|
|
|
|
import (
|
2020-07-17 02:40:23 +02:00
|
|
|
"database/sql"
|
2020-04-10 04:40:22 +02:00
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/go-chi/chi"
|
|
|
|
"github.com/google/uuid"
|
2020-08-07 03:50:35 +02:00
|
|
|
"github.com/jordanknott/taskcafe/internal/auth"
|
|
|
|
"github.com/jordanknott/taskcafe/internal/db"
|
2020-04-10 04:40:22 +02:00
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
|
|
|
)
|
|
|
|
|
2020-08-07 03:50:35 +02:00
|
|
|
var jwtKey = []byte("taskcafe_test_key")
|
2020-04-10 04:40:22 +02:00
|
|
|
|
|
|
|
type authResource struct{}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// LoginRequestData is the request data when a user logs in
|
2020-07-17 02:40:23 +02:00
|
|
|
type LoginRequestData struct {
|
|
|
|
Username string
|
|
|
|
Password string
|
2020-07-05 01:02:57 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// NewUserAccount is the request data for a new user
|
2020-07-17 02:40:23 +02:00
|
|
|
type NewUserAccount struct {
|
|
|
|
FullName string `json:"fullname"`
|
2020-07-05 01:02:57 +02:00
|
|
|
Username string
|
|
|
|
Password string
|
2020-07-17 02:40:23 +02:00
|
|
|
Initials string
|
|
|
|
Email string
|
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// InstallRequestData is the request data for installing new Taskcafe app
|
2020-07-17 02:40:23 +02:00
|
|
|
type InstallRequestData struct {
|
|
|
|
User NewUserAccount
|
2020-07-05 01:02:57 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// LoginResponseData is the response data for when a user logs in
|
2020-07-05 01:02:57 +02:00
|
|
|
type LoginResponseData struct {
|
|
|
|
AccessToken string `json:"accessToken"`
|
2020-07-17 02:40:23 +02:00
|
|
|
IsInstalled bool `json:"isInstalled"`
|
2020-07-05 01:02:57 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// LogoutResponseData is the response data for when a user logs out
|
2020-07-05 01:02:57 +02:00
|
|
|
type LogoutResponseData struct {
|
|
|
|
Status string `json:"status"`
|
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// RefreshTokenResponseData is the response data for when an access token is refreshed
|
2020-07-05 01:02:57 +02:00
|
|
|
type RefreshTokenResponseData struct {
|
|
|
|
AccessToken string `json:"accessToken"`
|
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// AvatarUploadResponseData is the response data for a user profile is uploaded
|
2020-07-05 01:02:57 +02:00
|
|
|
type AvatarUploadResponseData struct {
|
|
|
|
UserID string `json:"userID"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// RefreshTokenHandler handles when a user attempts to refresh token
|
2020-08-07 03:50:35 +02:00
|
|
|
func (h *TaskcafeHandler) RefreshTokenHandler(w http.ResponseWriter, r *http.Request) {
|
2020-07-17 02:40:23 +02:00
|
|
|
_, err := h.repo.GetSystemOptionByKey(r.Context(), "is_installed")
|
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
user, err := h.repo.GetUserAccountByUsername(r.Context(), "system")
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
2020-08-01 03:01:14 +02:00
|
|
|
accessTokenString, err := auth.NewAccessToken(user.UserID.String(), auth.InstallOnly, user.RoleCode)
|
2020-07-17 02:40:23 +02:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
w.Header().Set("Content-type", "application/json")
|
|
|
|
json.NewEncoder(w).Encode(LoginResponseData{AccessToken: accessTokenString, IsInstalled: false})
|
|
|
|
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
log.WithError(err).Error("get system option")
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-10 04:40:22 +02:00
|
|
|
c, err := r.Cookie("refreshToken")
|
|
|
|
if err != nil {
|
|
|
|
if err == http.ErrNoCookie {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2020-07-17 02:40:23 +02:00
|
|
|
log.WithError(err).Error("unknown error")
|
2020-04-10 04:40:22 +02:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
refreshTokenID := uuid.MustParse(c.Value)
|
|
|
|
token, err := h.repo.GetRefreshTokenByID(r.Context(), refreshTokenID)
|
|
|
|
if err != nil {
|
2020-07-17 02:40:23 +02:00
|
|
|
if err == sql.ErrNoRows {
|
|
|
|
|
|
|
|
log.WithError(err).WithFields(log.Fields{"refreshTokenID": refreshTokenID.String()}).Error("no tokens found")
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.WithError(err).Error("token retrieve failure")
|
2020-04-10 04:40:22 +02:00
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-08-01 03:01:14 +02:00
|
|
|
user, err := h.repo.GetUserAccountByID(r.Context(), token.UserID)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("user retrieve failure")
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-04-10 04:40:22 +02:00
|
|
|
refreshCreatedAt := time.Now().UTC()
|
|
|
|
refreshExpiresAt := refreshCreatedAt.AddDate(0, 0, 1)
|
2020-07-05 01:02:57 +02:00
|
|
|
refreshTokenString, err := h.repo.CreateRefreshToken(r.Context(), db.CreateRefreshTokenParams{token.UserID, refreshCreatedAt, refreshExpiresAt})
|
2020-04-10 04:40:22 +02:00
|
|
|
|
|
|
|
err = h.repo.DeleteRefreshTokenByID(r.Context(), token.TokenID)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
2020-08-01 03:01:14 +02:00
|
|
|
accessTokenString, err := auth.NewAccessToken(token.UserID.String(), auth.Unrestricted, user.RoleCode)
|
2020-04-10 04:40:22 +02:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-type", "application/json")
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
|
|
Name: "refreshToken",
|
|
|
|
Value: refreshTokenString.TokenID.String(),
|
|
|
|
Expires: refreshExpiresAt,
|
|
|
|
HttpOnly: true,
|
|
|
|
})
|
2020-07-17 02:40:23 +02:00
|
|
|
json.NewEncoder(w).Encode(LoginResponseData{AccessToken: accessTokenString, IsInstalled: true})
|
2020-04-10 04:40:22 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// LogoutHandler removes all refresh tokens to log out user
|
2020-08-07 03:50:35 +02:00
|
|
|
func (h *TaskcafeHandler) LogoutHandler(w http.ResponseWriter, r *http.Request) {
|
2020-04-20 05:02:55 +02:00
|
|
|
c, err := r.Cookie("refreshToken")
|
|
|
|
if err != nil {
|
|
|
|
if err == http.ErrNoCookie {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
refreshTokenID := uuid.MustParse(c.Value)
|
|
|
|
err = h.repo.DeleteRefreshTokenByID(r.Context(), refreshTokenID)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
json.NewEncoder(w).Encode(LogoutResponseData{Status: "success"})
|
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// LoginHandler creates a new refresh & access token for the user if given the correct credentials
|
2020-08-07 03:50:35 +02:00
|
|
|
func (h *TaskcafeHandler) LoginHandler(w http.ResponseWriter, r *http.Request) {
|
2020-04-10 04:40:22 +02:00
|
|
|
var requestData LoginRequestData
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&requestData)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
log.Debug("bad request body")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := h.repo.GetUserAccountByUsername(r.Context(), requestData.Username)
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
|
|
|
"username": requestData.Username,
|
|
|
|
}).Warn("user account not found")
|
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(requestData.Password))
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(log.Fields{
|
2020-08-21 01:11:24 +02:00
|
|
|
"username": requestData.Username,
|
|
|
|
}).Warn("password incorrect for user")
|
2020-04-10 04:40:22 +02:00
|
|
|
w.WriteHeader(http.StatusUnauthorized)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshCreatedAt := time.Now().UTC()
|
|
|
|
refreshExpiresAt := refreshCreatedAt.AddDate(0, 0, 1)
|
2020-07-05 01:02:57 +02:00
|
|
|
refreshTokenString, err := h.repo.CreateRefreshToken(r.Context(), db.CreateRefreshTokenParams{user.UserID, refreshCreatedAt, refreshExpiresAt})
|
2020-04-10 04:40:22 +02:00
|
|
|
|
2020-08-01 03:01:14 +02:00
|
|
|
accessTokenString, err := auth.NewAccessToken(user.UserID.String(), auth.Unrestricted, user.RoleCode)
|
2020-07-17 02:40:23 +02:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
|
|
|
|
w.Header().Set("Content-type", "application/json")
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
|
|
Name: "refreshToken",
|
|
|
|
Value: refreshTokenString.TokenID.String(),
|
|
|
|
Expires: refreshExpiresAt,
|
|
|
|
HttpOnly: true,
|
|
|
|
})
|
|
|
|
json.NewEncoder(w).Encode(LoginResponseData{accessTokenString, false})
|
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// InstallHandler creates first user on fresh install
|
2020-08-07 03:50:35 +02:00
|
|
|
func (h *TaskcafeHandler) InstallHandler(w http.ResponseWriter, r *http.Request) {
|
2020-07-17 02:40:23 +02:00
|
|
|
if restricted, ok := r.Context().Value("restricted_mode").(auth.RestrictedMode); ok {
|
|
|
|
if restricted != auth.InstallOnly {
|
|
|
|
log.Warning("attempted to install without install only restriction")
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := h.repo.GetSystemOptionByKey(r.Context(), "is_installed")
|
|
|
|
if err != sql.ErrNoRows {
|
|
|
|
log.WithError(err).Error("install handler called even though system is installed")
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var requestData InstallRequestData
|
|
|
|
err = json.NewDecoder(r.Body).Decode(&requestData)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
createdAt := time.Now().UTC()
|
|
|
|
hashedPwd, err := bcrypt.GenerateFromPassword([]byte(requestData.User.Password), 14)
|
|
|
|
user, err := h.repo.CreateUserAccount(r.Context(), db.CreateUserAccountParams{
|
2020-07-17 04:57:02 +02:00
|
|
|
FullName: requestData.User.FullName,
|
2020-07-17 02:40:23 +02:00
|
|
|
Username: requestData.User.Username,
|
|
|
|
Initials: requestData.User.Initials,
|
|
|
|
Email: requestData.User.Email,
|
|
|
|
PasswordHash: string(hashedPwd),
|
|
|
|
CreatedAt: createdAt,
|
|
|
|
RoleCode: "admin",
|
|
|
|
})
|
|
|
|
|
|
|
|
_, err = h.repo.CreateSystemOption(r.Context(), db.CreateSystemOptionParams{Key: "is_installed", Value: sql.NullString{Valid: true, String: "true"}})
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
refreshCreatedAt := time.Now().UTC()
|
|
|
|
refreshExpiresAt := refreshCreatedAt.AddDate(0, 0, 1)
|
|
|
|
refreshTokenString, err := h.repo.CreateRefreshToken(r.Context(), db.CreateRefreshTokenParams{user.UserID, refreshCreatedAt, refreshExpiresAt})
|
|
|
|
|
2020-09-12 10:24:09 +02:00
|
|
|
log.WithField("userID", user.UserID.String()).Info("creating install access token")
|
2020-08-01 03:01:14 +02:00
|
|
|
accessTokenString, err := auth.NewAccessToken(user.UserID.String(), auth.Unrestricted, user.RoleCode)
|
2020-04-10 04:40:22 +02:00
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
2020-09-12 10:24:09 +02:00
|
|
|
log.Info(accessTokenString)
|
2020-04-10 04:40:22 +02:00
|
|
|
|
|
|
|
w.Header().Set("Content-type", "application/json")
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
|
|
Name: "refreshToken",
|
|
|
|
Value: refreshTokenString.TokenID.String(),
|
|
|
|
Expires: refreshExpiresAt,
|
|
|
|
HttpOnly: true,
|
|
|
|
})
|
2020-07-17 02:40:23 +02:00
|
|
|
json.NewEncoder(w).Encode(LoginResponseData{accessTokenString, false})
|
2020-04-10 04:40:22 +02:00
|
|
|
}
|
|
|
|
|
2020-08-21 01:11:24 +02:00
|
|
|
// Routes registers all authentication routes
|
2020-08-07 03:50:35 +02:00
|
|
|
func (rs authResource) Routes(taskcafeHandler TaskcafeHandler) chi.Router {
|
2020-04-10 04:40:22 +02:00
|
|
|
r := chi.NewRouter()
|
2020-08-07 03:50:35 +02:00
|
|
|
r.Post("/login", taskcafeHandler.LoginHandler)
|
|
|
|
r.Post("/refresh_token", taskcafeHandler.RefreshTokenHandler)
|
|
|
|
r.Post("/logout", taskcafeHandler.LogoutHandler)
|
2020-04-10 04:40:22 +02:00
|
|
|
return r
|
|
|
|
}
|