forked from dionyself/golang-cms
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.go
145 lines (125 loc) · 3.2 KB
/
auth.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
package admins
import (
"fmt"
"github.com/chuongnx/golang-cms/models"
"github.com/chuongnx/golang-cms/utils"
"github.com/chuongnx/beego"
"github.com/chuongnx/beego/context"
"github.com/chuongnx/beego/orm"
)
var sessionName = beego.AppConfig.String("SessionName")
// LoginController ...
type LoginController struct {
BaseController
}
// LoginView ...
func (CTRL *LoginController) LoginView() {
CTRL.ConfigPage("login.html")
}
// Login authenticates the user
func (CTRL *LoginController) Login() {
username := CTRL.GetString("username")
password := CTRL.GetString("password")
backTo := CTRL.GetString("back_to")
var user models.User
if VerifyUser(&user, username, password) {
CTRL.SetSession(sessionName, user.Id)
if backTo != "" {
CTRL.Redirect("/admin/"+backTo, 302)
} else {
CTRL.Redirect("/admin/profile/0/show", 302)
}
} else {
CTRL.Redirect("/admin/register", 302)
}
}
// Logout ...
func (CTRL *LoginController) Logout() {
CTRL.DelSession(sessionName)
CTRL.Redirect("/admin/login", 302)
}
// RegisterView displays register form
func (CTRL *LoginController) RegisterView() {
CTRL.ConfigPage("register.html")
}
// Register the user
func (CTRL *LoginController) Register() {
form := new(models.RegisterForm)
if err := CTRL.ParseForm(form); err != nil {
CTRL.Abort("401")
}
if form.Validate() {
salt := utils.GetRandomString(10)
encodedPwd := salt + "$" + utils.EncodePassword(form.Password, salt)
o := CTRL.GetDB()
profile := new(models.Profile)
profile.Age = 0
profile.Avatar = "female"
if form.Gender {
profile.Avatar = "male"
}
profile.Gender = form.Gender
user := new(models.User)
user.Profile = profile
user.Username = form.Username
user.Password = encodedPwd
user.Rands = salt
fmt.Println(o.Insert(profile))
fmt.Println(o.Insert(user))
CTRL.Redirect("/admin/", 302)
} else {
CTRL.Data["form"] = form
for key, msg := range form.InvalidFields {
fmt.Println(key, msg)
}
CTRL.ConfigPage("register.html")
}
}
// HasUser checks if user exists in db
func HasUser(user *models.User, username string) bool {
var err error
qs := orm.NewOrm()
user.Username = username
err = qs.Read(user, "Username")
if err == nil {
return true
}
return false
}
// VerifyPassword checks if pwd is correct
func VerifyPassword(rawPwd, encodedPwd string) bool {
var salt, encoded string
salt = encodedPwd[:10]
encoded = encodedPwd[11:]
return utils.EncodePassword(rawPwd, salt) == encoded
}
// VerifyUser virifies user credentials
func VerifyUser(user *models.User, username, password string) (success bool) {
if HasUser(user, username) == false {
return
}
print("test")
if VerifyPassword(password, user.Password) {
success = true
}
return
}
// AuthRequest "filter" to limit request based on sessionid
var AuthRequest = func(ctx *context.Context) {
uid, ok := ctx.Input.Session(sessionName).(int)
if !ok && ctx.Input.URI() != "/admin/login" && ctx.Input.URI() != "/admin/register" {
ctx.Redirect(302, "/admin/login")
return
}
var user models.User
var err error
qs := orm.NewOrm()
user.Id = uid
err = qs.Read(&user, "Id")
if err != nil {
ctx.Redirect(302, "/admin/login")
return
}
qs.LoadRelated(&user, "Profile")
ctx.Input.SetData("user", user)
}