mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 18:37:05 +00:00
Version 3.0.0-beta cleaned
This commit is contained in:
97
plugin/iriscontrol/user_auth.go
Normal file
97
plugin/iriscontrol/user_auth.go
Normal file
@@ -0,0 +1,97 @@
|
||||
package iriscontrol
|
||||
|
||||
import (
|
||||
"strings"
|
||||
|
||||
"github.com/kataras/iris"
|
||||
"github.com/kataras/iris/sessions"
|
||||
// _ empty because it auto-registers
|
||||
_ "github.com/kataras/iris/sessions/providers/memory"
|
||||
)
|
||||
|
||||
var panelSessions *sessions.Manager
|
||||
|
||||
func init() {
|
||||
//using the default
|
||||
panelSessions = sessions.New()
|
||||
}
|
||||
|
||||
type user struct {
|
||||
username string
|
||||
password string
|
||||
}
|
||||
type userAuth struct {
|
||||
authenticatedUsers []user
|
||||
}
|
||||
|
||||
// newUserAuth returns a new userAuth object, parameter is the authenticated users as map
|
||||
func newUserAuth(usersMap map[string]string) *userAuth {
|
||||
if usersMap != nil {
|
||||
obj := &userAuth{make([]user, 0)}
|
||||
for key, val := range usersMap {
|
||||
obj.authenticatedUsers = append(obj.authenticatedUsers, user{key, val})
|
||||
}
|
||||
|
||||
return obj
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (u *userAuth) login(ctx *iris.Context) {
|
||||
session := panelSessions.Start(ctx)
|
||||
|
||||
username := ctx.PostFormValue("username")
|
||||
password := ctx.PostFormValue("password")
|
||||
|
||||
for _, authenticatedUser := range u.authenticatedUsers {
|
||||
if authenticatedUser.username == username && authenticatedUser.password == password {
|
||||
session.Set("username", username)
|
||||
session.Set("password", password)
|
||||
ctx.Write("success")
|
||||
return
|
||||
}
|
||||
}
|
||||
ctx.Write("fail")
|
||||
|
||||
}
|
||||
|
||||
func (u *userAuth) logout(ctx *iris.Context) {
|
||||
session := panelSessions.Start(ctx)
|
||||
session.Set("user", nil)
|
||||
|
||||
ctx.Redirect("/login")
|
||||
}
|
||||
|
||||
// check if session stored, then check if this user is the correct, each time, then continue, else not
|
||||
func (u *userAuth) Serve(ctx *iris.Context) {
|
||||
if ctx.PathString() == "/login" || strings.HasPrefix(ctx.PathString(), "/public") {
|
||||
ctx.Next()
|
||||
return
|
||||
}
|
||||
session := panelSessions.Start(ctx)
|
||||
|
||||
if sessionVal := session.Get("username"); sessionVal != nil {
|
||||
username := sessionVal.(string)
|
||||
password := session.GetString("password")
|
||||
if username != "" && password != "" {
|
||||
|
||||
for _, authenticatedUser := range u.authenticatedUsers {
|
||||
if authenticatedUser.username == username && authenticatedUser.password == password {
|
||||
ctx.Next()
|
||||
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
//if not logged in the redirect to the /login
|
||||
ctx.Redirect("/login")
|
||||
|
||||
}
|
||||
|
||||
// Destroy this is called on PreClose by the iriscontrol.go
|
||||
func (u *userAuth) Destroy() {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user