aboutsummaryrefslogtreecommitdiff
path: root/api/user.go
blob: 4d4edba8b49ef139047bd4f9149d48746f7ec6b7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package api

import (
	"fmt"
	"regexp"

	"github.com/gin-gonic/gin"

	"immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/db"
)

const (
	VALID_EMAIL_REGEX = `(?i)^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$`
)

func IsValidEmailAddress(email string) bool {
	r := regexp.MustCompile(VALID_EMAIL_REGEX)

	return r.MatchString(email)
}

type SignParams struct {
	Email    string
	Password string
}

type SignResult struct {
	Token string `json:"token"`
}

func (s SignParams) Validate() *Error {
	if !IsValidEmailAddress(s.Email) {
		return &Error{InvalidEmail, "invalid email", fmt.Errorf("'%v' is not a valid email", s.Email)}
	}

	if s.Password == "" {
		return &Error{InvalidPassword, "invalid password", fmt.Errorf("invalid password")}
	}

	return nil
}

type SignupQuery struct {
	In SignParams
}

func (q SignupQuery) ValidateParams() *Error {
	return q.In.Validate()
}

func (q SignupQuery) Run() (interface{}, *Error) {
	user, err := db.GetUserByEmail(q.In.Email)
	if err != nil {
		return nil, NewInternalError(err)
	}

	if user != nil {
		return nil, &Error{EmailExists, "email already taken", fmt.Errorf("'%v' is already registered '%v'", q.In.Email, user)}
	}

	newUser := db.User{Email: q.In.Email, Status: db.AwaitingConfirmation}
	newUser.PasswordHash, err = db.HashPassword(q.In.Password)
	if err != nil {
		return nil, NewInternalError(err)
	}

	err = db.InsertUser(&newUser)
	if err != nil {
		return nil, NewInternalError(err)
	}

	token, err := CreateJwtToken(newUser.Id)
	if err != nil {
		return nil, NewInternalError(fmt.Errorf("cannot create jwt token %v", err))
	}

	return SignResult{token}, nil
}

type SigninQuery struct {
	In SignParams
}

func (q SigninQuery) ValidateParams() *Error {
	return q.In.Validate()
}

func (q SigninQuery) Run() (interface{}, *Error) {
	user, err := db.GetUserByEmail(q.In.Email)
	if err != nil {
		return nil, NewInternalError(err)
	}

	if user == nil {
		return nil, &Error{InvalidCredentials, "invalid credentials", fmt.Errorf("no email '%v' found", q.In.Email)}
	}

	err = db.ValidatePassword(q.In.Password, user.PasswordHash)
	if err != nil {
		return nil, &Error{InvalidCredentials, "invalid credentials", err}
	}

	if user.Status != db.Confirmed {
		return nil, &Error{UserNotConfirmed, "user awaiting admin validation", fmt.Errorf("user '%v' not confirmed", user)}
	}

	token, err := CreateJwtToken(user.Id)
	if err != nil {
		return nil, NewInternalError(err)
	}

	return SignResult{token}, nil
}

func UserConfirmed(c *gin.Context) *Error {
	user, exists := c.Get("user")

	if !exists {
		return &Error{NotAuthorized, "not authorized", fmt.Errorf("no user key in context")}
	}

	if user.(db.User).Status != db.Confirmed {
		return &Error{UserNotConfirmed, "user awaiting admin validation", fmt.Errorf("user '%v' not confirmed", user)}
	}

	return nil
}

func GetUser(c *gin.Context) db.User {
	user, _ := c.Get("user")

	return user.(db.User)
}