This repository has been archived by the owner on Sep 13, 2022. It is now read-only.
forked from MCWertGaming/authfox
/
register.go
157 lines (135 loc) · 4.93 KB
/
register.go
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/* <AuthFox - a simple authentication and session server for Puroto>
Copyright (C) 2022 PurotoApp
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package endpoints
import (
"errors"
"net/http"
"strings"
"time"
"github.com/PurotoApp/authfox/internal/helper"
"github.com/PurotoApp/libpuroto/libpuroto"
"github.com/gin-gonic/gin"
"github.com/go-redis/redis"
"github.com/google/uuid"
"gorm.io/gorm"
)
// This struct stores user informations send to the register API endpoint
type sendUserProfile struct {
// the send user name
NameFormat string `json:"user_name"`
// Plain-text password received from client
Password string `json:"password"`
// account email
Email string `json:"email"`
}
// This is like sessionPair but without the session type switch
type returnSession struct {
UserID string `json:"uid"`
Token string `json:"token"`
}
func registerUser(pg_conn *gorm.DB, redisVerify, redisSession *redis.Client) gin.HandlerFunc {
return func(c *gin.Context) {
// only answer if content-type is set right
if !libpuroto.JsonRequested(c) {
return
}
var sendUserStruct sendUserProfile
// put the json into the struct
if err := c.BindJSON(&sendUserStruct); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
libpuroto.LogError("authfox", err)
return
}
// make sure that the received values are legal
if !checkSendUserProfile(&sendUserStruct) {
c.AbortWithStatus(http.StatusBadRequest)
libpuroto.LogEvent("authfox", "registerUser(): Received invalid or illegal registration data")
return
}
// check if the given email or user name already exists
result := pg_conn.Where("name_static = ?", strings.ToLower(sendUserStruct.NameFormat)).Where("email = ?", strings.ToLower(sendUserStruct.Email)).Take(&Verify{})
if result.Error != nil && !errors.Is(result.Error, gorm.ErrRecordNotFound) {
c.AbortWithStatus(http.StatusInternalServerError)
libpuroto.LogError("authfox", result.Error)
return
} else if result.RowsAffected > 0 {
c.AbortWithStatus(http.StatusBadRequest)
libpuroto.LogEvent("authfox", "Received user that already exists")
return
}
// prepare saving of user data into verify DB
var userData Verify
// hash the password
hash, err := helper.CreateHash(&sendUserStruct.Password)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
libpuroto.LogError("authfox", err)
return
}
// safe the hashed password
userData.Password = hash
// remove the old password from memory
sendUserStruct.Password = ""
// fill other user data
userData.NameFormat = sendUserStruct.NameFormat
userData.NameStatic = strings.ToLower(sendUserStruct.NameFormat)
userData.Email = strings.ToLower(sendUserStruct.Email)
userData.RegisterIP = c.ClientIP()
userData.RegisterTime = time.Now()
if userData.VerifyCode, err = libpuroto.RandomString(32); err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
libpuroto.LogError("authfox", err)
return
}
// create user ID
userData.UserID = uuid.New().String()
// store into DB
if err = pg_conn.Create(&userData).Error; err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
libpuroto.LogError("authfox", err)
return
}
// create session
session, err := helper.CreateSession(&userData.UserID, redisVerify, redisSession, true)
if err != nil {
c.AbortWithStatus(http.StatusInternalServerError)
libpuroto.LogError("authfox", err)
return
}
// remove the session type as it is always true
var basicSession returnSession
basicSession.UserID = session.UserID
basicSession.Token = session.Token
c.JSON(http.StatusAccepted, basicSession)
}
}
// check the send user data for correctnes and forbidden values
// TODO: move into Guardian service
func checkSendUserProfile(profile *sendUserProfile) bool {
// TODO: refuse if the name contains slurs / forbidden words
// TODO: don't allow special characters:
if strings.Count(profile.NameFormat, "") < 6 || strings.Count(profile.NameFormat, "") > 32 ||
strings.Count(profile.NameFormat, "@") > 0 || strings.Count(profile.NameFormat, " ") > 0 {
return false
}
// TODO: refuse if the email address is forbidden (trashmail etc)
if profile.Email == "" || !libpuroto.CheckEmail(profile.Email) {
return false
}
// TODO: refuse on weak passwords
if strings.Count(profile.Password, "") < 9 || len(profile.Password) > 512 {
return false
}
return true
}