/
rest_register.go
141 lines (119 loc) · 3.34 KB
/
rest_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
package rest
import (
"encoding/json"
"fmt"
"multiorgledger/blockchain/invoke"
"multiorgledger/web/model"
"net/http"
"strings"
"unicode"
)
func (app *RestApp) RegisterHandler(w http.ResponseWriter, r *http.Request) {
var userdata model.ModelUserData
_ = json.NewDecoder(r.Body).Decode(&userdata)
orgName := userdata.Org
email := userdata.Email
name := userdata.Name
mobile := userdata.Mobile
age := userdata.Age
salary := userdata.Salary
role := userdata.Role
password := hash(userdata.Password)
verifyErr := verifyPassword(userdata.Password)
fmt.Println(" ####### Rest Input for Register ####### ")
fmt.Println(" Email = " + email)
fmt.Println(" Password = " + password)
fmt.Println(" Role = " + role)
fmt.Println(" Name = " + name)
fmt.Println(" Mobile = " + mobile)
fmt.Println(" Age = " + age)
fmt.Println(" Salary = " + salary)
fmt.Println(" ###################################### ")
if verifyErr != nil && len(verifyErr.Error()) > 0 {
respondJSON(w, map[string]string{"error": verifyErr.Error(), "message": "Password must contain at least one number and one uppercase and lowercase letter, and at least 8 or more characters"})
}
Org, err := app.Org.InitializeOrg(orgName)
if err != nil {
respondJSON(w, map[string]string{"error": "failed to invoke user " + err.Error()})
}
orgUser, err := Org.RegisterUserWithCA(orgName, email, password, role)
orgInvoke := invoke.OrgInvoke{
User: orgUser,
}
if err != nil {
respondJSON(w, map[string]string{"error": "Unable to Register : " + err.Error()})
} else {
token := app.processAuthentication(w, email)
if len(token) > 0 {
err := orgInvoke.InvokeCreateUser(name, age, mobile, salary)
if err != nil {
respondJSON(w, map[string]string{"error": "failed to invoke user " + err.Error()})
} else {
respondJSON(w, map[string]string{
"token": token,
"name": name,
"email": email,
"age": age,
"mobile": mobile,
"salary": salary,
})
}
} else {
respondJSON(w, map[string]string{"error": "Failed to generate token"})
}
}
}
func verifyPassword(password string) error {
var uppercasePresent bool
var lowercasePresent bool
var numberPresent bool
var specialCharPresent bool
const minPassLength = 8
const maxPassLength = 64
var passLen int
var errorString string
for _, ch := range password {
switch {
case unicode.IsNumber(ch):
numberPresent = true
passLen++
case unicode.IsUpper(ch):
uppercasePresent = true
passLen++
case unicode.IsLower(ch):
lowercasePresent = true
passLen++
case unicode.IsPunct(ch) || unicode.IsSymbol(ch):
specialCharPresent = true
passLen++
case ch == ' ':
passLen++
}
}
appendError := func(err string) {
if len(strings.TrimSpace(errorString)) != 0 {
errorString += ", " + err
} else {
errorString = err
}
}
if !lowercasePresent {
appendError("lowercase letter missing")
}
if !uppercasePresent {
appendError("uppercase letter missing")
}
if !numberPresent {
appendError("atleast one numeric character required")
}
if !specialCharPresent {
appendError("special character missing")
}
if !(minPassLength <= passLen && passLen <= maxPassLength) {
appendError(fmt.Sprintf("password length must be between %d to %d characters long", minPassLength, maxPassLength))
}
if len(errorString) != 0 {
return fmt.Errorf(errorString)
}
return nil
}