-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add an example for sessions + view data as requested
- Loading branch information
Showing
9 changed files
with
241 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) { | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 => ../../../../ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package main | ||
|
||
import ( | ||
"github.com/kataras/iris/v12" | ||
"github.com/kataras/iris/v12/sessions" | ||
) | ||
|
||
func main() { | ||
app := iris.New() | ||
app.RegisterView(iris.HTML("./views", ".html")) | ||
|
||
sess := sessions.New(sessions.Config{Cookie: "session_cookie", AllowReclaim: true}) | ||
app.Use(sess.Handler()) | ||
// ^ use app.UseRouter instead to access sessions on HTTP errors too. | ||
|
||
// Register our custom middleware, after the sessions middleware. | ||
app.Use(setSessionViewData) | ||
|
||
app.Get("/", index) | ||
app.Listen(":8080") | ||
} | ||
|
||
func setSessionViewData(ctx iris.Context) { | ||
session := sessions.Get(ctx) | ||
ctx.ViewData("session", session) | ||
ctx.Next() | ||
} | ||
|
||
func index(ctx iris.Context) { | ||
session := sessions.Get(ctx) | ||
session.Set("username", "kataras") | ||
ctx.View("index") | ||
/* OR without middleware: | ||
ctx.View("index", iris.Map{ | ||
"session": session, | ||
// {{.session.Get "username"}} | ||
// OR to pass only the 'username': | ||
// "username": session.Get("username"), | ||
// {{.username}} | ||
}) | ||
*/ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Sessions View Data</title> | ||
</head> | ||
<body> | ||
Hello {{.session.Get "username"}} | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters