Start refactoring
This commit is contained in:
@@ -3,94 +3,88 @@ package server
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"opensavecloudserver/authentication"
|
||||
"time"
|
||||
)
|
||||
|
||||
type Credential struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
type (
|
||||
userLogin struct {
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
userRegistration struct {
|
||||
UserUsername string `json:"username"`
|
||||
UserPassword string `json:"password"`
|
||||
UserDisplayName string `json:"displayName"`
|
||||
}
|
||||
|
||||
userPresenter struct {
|
||||
ID string `json:"id"`
|
||||
Username string `json:"username"`
|
||||
DisplayName string `json:"displayName"`
|
||||
}
|
||||
|
||||
jwtPresenter struct {
|
||||
Token []byte `json:"token"`
|
||||
}
|
||||
)
|
||||
|
||||
func (ur userRegistration) Username() string {
|
||||
return ur.UserUsername
|
||||
}
|
||||
|
||||
type TokenValidation struct {
|
||||
Valid bool `json:"valid"`
|
||||
func (ur userRegistration) Password() string {
|
||||
return ur.UserPassword
|
||||
}
|
||||
|
||||
func Login(w http.ResponseWriter, r *http.Request) {
|
||||
func (ur userRegistration) DisplayName() string {
|
||||
return ur.UserDisplayName
|
||||
}
|
||||
|
||||
func (s *HTTPServer) loginUserHandler(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
internalServerError(w, r)
|
||||
log.Println(err)
|
||||
return
|
||||
panic(err)
|
||||
}
|
||||
credential := new(Credential)
|
||||
err = json.Unmarshal(body, credential)
|
||||
|
||||
var ul userLogin
|
||||
err = json.Unmarshal(body, &ul)
|
||||
if err != nil {
|
||||
internalServerError(w, r)
|
||||
log.Println(err)
|
||||
return
|
||||
panic(err)
|
||||
}
|
||||
token, err := authentication.Connect(credential.Username, credential.Password)
|
||||
|
||||
jwt, err := s.deps.Authenticator.Authenticate(ul.Username, ul.Password)
|
||||
if err != nil {
|
||||
unauthorized(w, r)
|
||||
return
|
||||
}
|
||||
ok(token, w, r)
|
||||
|
||||
payload := jwtPresenter{
|
||||
Token: jwt,
|
||||
}
|
||||
ok(payload, w)
|
||||
}
|
||||
|
||||
func Register(w http.ResponseWriter, r *http.Request) {
|
||||
func (s *HTTPServer) registerUserHandler(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
internalServerError(w, r)
|
||||
log.Println(err)
|
||||
return
|
||||
panic(err)
|
||||
}
|
||||
registration := new(authentication.Registration)
|
||||
err = json.Unmarshal(body, registration)
|
||||
if err != nil {
|
||||
internalServerError(w, r)
|
||||
log.Println(err)
|
||||
return
|
||||
}
|
||||
err = authentication.Register(registration)
|
||||
if err != nil {
|
||||
badRequest(err.Error(), w, r)
|
||||
return
|
||||
}
|
||||
payload := successMessage{
|
||||
Message: "You are now registered",
|
||||
Timestamp: time.Now(),
|
||||
Status: 200,
|
||||
}
|
||||
ok(payload, w, r)
|
||||
}
|
||||
|
||||
func CheckToken(w http.ResponseWriter, r *http.Request) {
|
||||
body, err := io.ReadAll(r.Body)
|
||||
var ur userRegistration
|
||||
err = json.Unmarshal(body, &ur)
|
||||
if err != nil {
|
||||
internalServerError(w, r)
|
||||
log.Println(err)
|
||||
return
|
||||
panic(err)
|
||||
}
|
||||
credential := new(authentication.AccessToken)
|
||||
err = json.Unmarshal(body, credential)
|
||||
|
||||
u, err := s.deps.UserRepository.CreateUser(ur)
|
||||
if err != nil {
|
||||
internalServerError(w, r)
|
||||
log.Println(err)
|
||||
return
|
||||
panic(err)
|
||||
}
|
||||
_, err = authentication.ParseToken(credential.Token)
|
||||
if err != nil {
|
||||
payload := TokenValidation{
|
||||
Valid: false,
|
||||
}
|
||||
ok(payload, w, r)
|
||||
return
|
||||
payload := userPresenter{
|
||||
ID: u.ID().String(),
|
||||
Username: u.Username(),
|
||||
DisplayName: u.DisplayName(),
|
||||
}
|
||||
payload := TokenValidation{
|
||||
Valid: true,
|
||||
}
|
||||
ok(payload, w, r)
|
||||
ok(payload, w)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user