-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
182 lines (149 loc) · 4.69 KB
/
main.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
package main
import (
"fmt"
"github.com/jinzhu/gorm"
_ "github.com/jinzhu/gorm/dialects/sqlite"
"github.com/qor/admin"
"github.com/qor/auth"
"github.com/qor/auth/auth_identity"
"github.com/qor/auth/authority"
"github.com/qor/auth/claims"
"github.com/qor/auth/providers/password"
"github.com/qor/qor"
"github.com/qor/redirect_back"
"github.com/qor/roles"
"github.com/qor/session/manager"
"net/http"
"qor-admin-auth-example/auththeme"
"strings"
"time"
)
// Product GORM-backend model
type Product struct {
gorm.Model
Name string
Description string
}
var (
// Initialize gorm DB
gormDB, _ = gorm.Open("sqlite3", "sample.db")
RedirectBack = redirect_back.New(&redirect_back.Config{
SessionManager: manager.SessionManager,
IgnoredPrefixes: []string{"/auth"},
})
Auth = auththeme.New(&auth.Config{
DB: gormDB,
UserModel: User{},
Redirector: auth.Redirector{RedirectBack},
})
// Authority initialize Authority for Authorization
Authority = authority.New(&authority.Config{
Auth: Auth,
})
)
type AdminAuth struct{}
func (AdminAuth) LoginURL(c *admin.Context) string {
return "/auth/login"
}
func (AdminAuth) LogoutURL(c *admin.Context) string {
return "/auth/logout"
}
func (AdminAuth) GetCurrentUser(c *admin.Context) qor.CurrentUser {
currentUser := Auth.GetCurrentUser(c.Request)
if currentUser != nil {
qorCurrentUser, ok := currentUser.(qor.CurrentUser)
if !ok {
fmt.Printf("User %#v haven't implement qor.CurrentUser interface\n", currentUser)
}
return qorCurrentUser
}
return nil
}
func init() {
roles.Register("admin", func(req *http.Request, currentUser interface{}) bool {
return currentUser != nil && currentUser.(*User).Role == "Admin"
})
}
var MyAuthorizeHandler = func(context *auth.Context) (*claims.Claims, error) {
var (
authInfo auth_identity.AuthIdentity
req = context.Request
tx = context.Auth.GetDB(req)
provider, _ = context.Provider.(*password.Provider)
)
req.ParseForm()
authInfo.Provider = provider.GetName()
authInfo.UID = strings.TrimSpace(req.Form.Get("login"))
if tx.Model(context.Auth.AuthIdentityModel).Where(authInfo).Scan(&authInfo).RecordNotFound() {
return nil, auth.ErrInvalidAccount
}
if provider.Config.Confirmable && authInfo.ConfirmedAt == nil {
currentUser, _ := context.Auth.UserStorer.Get(authInfo.ToClaims(), context)
provider.Config.ConfirmMailer(authInfo.UID, context, authInfo.ToClaims(), currentUser)
return nil, password.ErrUnconfirmed
}
if err := provider.Encryptor.Compare(authInfo.EncryptedPassword, strings.TrimSpace(req.Form.Get("password"))); err == nil {
return authInfo.ToClaims(), err
}
return nil, auth.ErrInvalidPassword
}
func createAdminUsers() {
AdminUser := &User{}
AdminUser.Email = "qortest@dev.com"
AdminUser.Confirmed = true
AdminUser.Name = "QOR Admin"
AdminUser.Role = "Admin"
gormDB.Create(AdminUser)
provider := (*auth.Auth).GetProvider(Auth, "password").(*password.Provider)
hashedPassword, _ := provider.Encryptor.Digest("qortest")
now := time.Now()
authIdentity := &auth_identity.AuthIdentity{}
authIdentity.Provider = "password"
authIdentity.UID = AdminUser.Email
authIdentity.EncryptedPassword = hashedPassword
authIdentity.UserID = fmt.Sprint(AdminUser.ID)
authIdentity.ConfirmedAt = &now
gormDB.Create(authIdentity)
// Send welcome notification
// Notification.Send(¬ification.Message{
// From: AdminUser,
// To: AdminUser,
// Title: "Welcome To QOR Admin",
// Body: "Welcome To QOR Admin",
// MessageType: "info",
// }, &qor.Context{DB: DraftDB})
}
func main() {
gormDB.LogMode(true)
gormDB.AutoMigrate(&User{}, &Product{}, &auth_identity.AuthIdentity{})
gormDB.Model(&User{}).AddUniqueIndex("idx_email", "Email")
gormDB.Model(&auth_identity.AuthIdentity{}).AddUniqueIndex("idx_uuid", "UID")
Admin := admin.New(&admin.AdminConfig{DB: gormDB, Auth: &AdminAuth{}})
Auth.RegisterProvider(password.New(&password.Config{
AuthorizeHandler: MyAuthorizeHandler,
}))
Admin.AddResource(&User{})
Admin.AddResource(&Product{})
createAdminUsers()
// Initalize an HTTP request multiplexer
mux := http.NewServeMux()
// Mount admin to the mux
Admin.MountTo("/admin", mux)
// Mount Auth to Router
mux.Handle("/auth/", Auth.NewServeMux())
Authority.Register("logged_in_half_hour", authority.Rule{TimeoutSinceLastLogin: time.Minute * 30})
fmt.Println("Listening on: 8080")
http.ListenAndServe(":8080", manager.SessionManager.Middleware(RedirectBack.Middleware(mux)))
}
type User struct {
gorm.Model
Email string `unique;form:"email"`
Password string
Name string `form:"name"`
Gender string
Role string
Confirmed bool
}
func (user User) DisplayName() string {
return user.Email
}