-
Notifications
You must be signed in to change notification settings - Fork 0
/
passport.go
226 lines (189 loc) · 4.09 KB
/
passport.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
package gop
import (
"gopkg.in/mgo.v2/bson"
"github.com/cheikhshift/db"
"github.com/gorilla/sessions"
"log"
"errors"
"crypto/sha256"
"github.com/jtblin/go-ldap-client"
"time"
"strings"
)
type User struct {
Id bson.ObjectId `bson:"_id,omitempty"`
Username string `valid:"unique,required"`
Pw [32]byte
Email string `valid:"email,unique,required"`
Created time.Time //timestamp local format
Scopes []string
Groups []string
Props db.O
LDAP bool
Attr map[string] string
}
var (
DB db.DB
DbName,UPage string
LDAPClient *ldap.LDAPClient
Zones []string
)
func UseLDAP(Ldap *ldap.LDAPClient){
LDAPClient = Ldap
}
func SetUnAuthPage(path string) {
UPage = path
}
func AddAuthZone (path string){
if Zones == nil {
Zones = [] string{}
}
if !Contains(Zones, path){
Zones = append(Zones, path)
}
}
func (u *User) SetPassword(pw string) {
u.Pw = sha256.Sum256([]byte(pw))
}
func (u *User) Push (ses *sessions.Session) error {
ses.Values["passport"] = u
if u.LDAP {
return nil
} else {
return DB.Save(u)
}
}
func New(u User) User {
return DB.New(&u).(User)
}
func Contains(arr []string, lookup string) bool {
for _, v := range arr {
if rIndex := strings.Index(lookup, v); (rIndex == 0 ) {
return true
}
}
return false
}
func GetUser(ses *sessions.Session) (*User, error) {
if _, ok := ses.Values["passport"]; ok {
return ses.Values["passport"].(*User), nil
} else {
return &User{}, errors.New("No session found.")
}
}
func Logout(ses *sessions.Session) (bool, error) {
if _, ok := ses.Values["passport"]; ok {
delete(ses.Values, "passport")
return true, nil
} else {
return false, errors.New("No session found.")
}
}
func Join(args ...interface{}) (bool, error) {
var email string
var ses *sessions.Session
if len(args) > 3 {
email = args[2].(string)
ses = args[3].(*sessions.Session)
} else {
ses = args[2].(*sessions.Session)
}
if _, ok := ses.Values["passport"]; !ok {
//delete(session.Values, "passport")
user := User{Username : args[0].(string)}
user.Pw = sha256.Sum256([]byte( args[1].(string) ))
user.Email = email
usr := DB.New(&user).(*User)
err := DB.Save(usr)
if err != nil {
return false, err
} else {
ses.Values["passport"] = usr
}
return true, nil
} else {
return false, errors.New("Already logged in!")
}
}
func Login(u string, pw string, ses *sessions.Session) (bool, error) {
if _, ok := ses.Values["passport"]; !ok {
//delete(session.Values, "passport")
user := User{}
err := DB.Query(user, bson.M{"username" : u , "pw" : sha256.Sum256([]byte(pw)) }).One(&user)
if err != nil {
return false, err
} else if user.Username == "" {
return false, errors.New("User not found!")
}
ses.Values["passport"] = &user
return true, nil
} else {
return false, errors.New("Already logged in!")
}
}
func LoginLDAP(u string, pw string, ses *sessions.Session) (bool, error) {
if _, ok := ses.Values["passport"]; !ok {
//delete(session.Values, "passport")
ok, user, err := LDAPClient.Authenticate(u, pw)
if err != nil {
return false, err
}
if !ok {
return false, err
}
Suser := User{ Username: u , Attr : user}
groups, err := LDAPClient.GetGroupsOfUser(u)
if err != nil {
return false, err
}
Suser.Groups = groups
Suser.LDAP = true
ses.Values["passport"] = &Suser
return true, nil
} else {
return false, errors.New("Already logged in!")
}
}
func RemoveAuthZone (path string){
if Zones == nil {
Zones = [] string{}
}
ns := []string{}
for _,v := range Zones {
if v != path {
ns = append(ns, v)
}
}
Zones = ns
}
func (u *User) AddZone(path string){
if u.Scopes == nil {
u.Scopes = [] string{}
}
if !Contains(u.Scopes, path){
u.Scopes = append(u.Scopes, path)
}
}
func (u *User) RemoveZone(path string){
if u.Scopes == nil {
u.Scopes = [] string{}
}
ns := []string{}
for _,v := range u.Scopes {
if v != path {
ns = append(ns, v)
}
}
u.Scopes = ns
}
func Connect(host string, dbname string){
dbs,err := db.Connect(host, dbname)
if err != nil {
log.Fatal(err)
}
DbName = dbname
SetDb(dbs)
}
func SetDb(db db.DB) {
DB = db
}