1
0
mirror of https://github.com/kataras/iris.git synced 2025-12-17 18:07:01 +00:00

add an example for sessions + view data as requested

This commit is contained in:
Gerasimos (Makis) Maropoulos
2020-11-02 18:46:38 +02:00
parent f1ebddb6d9
commit ed38047385
9 changed files with 241 additions and 12 deletions

View File

@@ -0,0 +1,25 @@
package main
import "github.com/kataras/iris/v12"
func loginView(ctx iris.Context) {
}
func login(ctx iris.Context) {
}
func logout(ctx iris.Context) {
ctx.Logout()
ctx.Redirect("/", iris.StatusTemporaryRedirect)
}
func createTodo(ctx iris.Context) {
}
func getTodo(ctx iris.Context) {
}

View File

@@ -0,0 +1,10 @@
module myapp
go 1.15
require (
github.com/kataras/iris/v12 v12.2.0-alpha.0.20201031040657-23d4c411cadb
github.com/google/uuid v1.1.2
)
replace github.com/kataras/iris/v12 => ../../../../

View File

@@ -0,0 +1,89 @@
package main
import (
"time"
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/middleware/jwt"
"github.com/kataras/iris/v12/middleware/jwt/blocklist/redis"
// Optionally to set token identifier.
"github.com/google/uuid"
)
var (
signatureSharedKey = []byte("sercrethatmaycontainch@r32length")
signer = jwt.NewSigner(jwt.HS256, signatureSharedKey, 15*time.Minute)
verifier = jwt.NewVerifier(jwt.HS256, signatureSharedKey)
)
func main() {
app := iris.New()
blocklist := redis.NewBlocklist()
verifier.Blocklist = blocklist
verifyMiddleware := verifier.Verify(func() interface{} {
return new(userClaims)
})
app.Get("/", loginView)
api := app.Party("/api")
{
api.Post("/login", login)
api.Post("/logout", verifyMiddleware, logout)
todoAPI := api.Party("/todos", verifyMiddleware)
{
todoAPI.Post("/", createTodo)
todoAPI.Get("/", listTodos)
todoAPI.Get("/{id:uint64}", getTodo)
}
}
protectedAPI := app.Party("/protected", verifyMiddleware)
protectedAPI.Get("/", protected)
protectedAPI.Get("/logout", logout)
// GET http://localhost:8080
// POST http://localhost:8080/api/login
// POST http://localhost:8080/api/logout
// POST http://localhost:8080/api/todos
// GET http://localhost:8080/api/todos
// GET http://localhost:8080/api/todos/{id}
app.Listen(":8080")
}
func authenticate(ctx iris.Context) {
claims := userClaims{
Username: "kataras",
}
// Generate JWT ID.
random, err := uuid.NewRandom()
if err != nil {
ctx.StopWithError(iris.StatusInternalServerError, err)
return
}
id := random.String()
// Set the ID with the jwt.ID.
token, err := signer.Sign(claims, jwt.ID(id))
if err != nil {
ctx.StopWithError(iris.StatusInternalServerError, err)
return
}
ctx.Write(token)
}
func protected(ctx iris.Context) {
claims := jwt.Get(ctx).(*userClaims)
// To the standard claims, e.g. the generated ID:
// jwt.GetVerifiedToken(ctx).StandardClaims.ID
ctx.WriteString(claims.Username)
}

View File

@@ -0,0 +1,63 @@
package main
import "golang.org/x/crypto/bcrypt"
func init() {
generateSampleUsers()
}
// User represents our User model.
type User struct {
ID uint64 `json:"id"`
Username string `json:"username"`
HashedPassword []byte `json:"-"`
}
// Users represents a user database.
// For the sake of the tutorial we use a simple slice of users.
var Users []User
func generateSampleUsers() {
Users = []User{
{ID: 1, Username: "vasiliki", HashedPassword: mustGeneratePassword("vasiliki_pass")}, // my grandmother.
{ID: 2, Username: "kataras", HashedPassword: mustGeneratePassword("kataras_pass")}, // me.
{ID: 3, Username: "george", HashedPassword: mustGeneratePassword("george_pass")}, // my young brother.
{ID: 4, Username: "kwstas", HashedPassword: mustGeneratePassword("kwstas_pass")}, // my youngest brother.
}
}
func fetchUser(username, password string) (User, bool) {
for _, u := range Users { // our example uses a static slice.
if u.Username == username {
// we compare the user input and the stored hashed password.
ok := ValidatePassword(password, u.HashedPassword)
if ok {
return u, true
}
}
}
return User{}, false
}
// mustGeneratePassword same as GeneratePassword but panics on errors.
func mustGeneratePassword(userPassword string) []byte {
hashed, err := GeneratePassword(userPassword)
if err != nil {
panic(err)
}
return hashed
}
// GeneratePassword will generate a hashed password for us based on the
// user's input.
func GeneratePassword(userPassword string) ([]byte, error) {
return bcrypt.GenerateFromPassword([]byte(userPassword), bcrypt.DefaultCost)
}
// ValidatePassword will check if passwords are matched.
func ValidatePassword(userPassword string, hashed []byte) bool {
err := bcrypt.CompareHashAndPassword(hashed, []byte(userPassword))
return err == nil
}