mirror of
https://github.com/kataras/iris.git
synced 2025-12-18 10:27:06 +00:00
add a jwt tutorial + go client
This commit is contained in:
7
_examples/auth/jwt/tutorial/util/app.go
Normal file
7
_examples/auth/jwt/tutorial/util/app.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package util
|
||||
|
||||
// Constants for the application.
|
||||
const (
|
||||
Version = "0.0.1"
|
||||
AppName = "myapp"
|
||||
)
|
||||
7
_examples/auth/jwt/tutorial/util/clock.go
Normal file
7
_examples/auth/jwt/tutorial/util/clock.go
Normal file
@@ -0,0 +1,7 @@
|
||||
package util
|
||||
|
||||
import "time"
|
||||
|
||||
// Now is the default current time for the whole application.
|
||||
// Can be modified for testing or custom timezone.
|
||||
var Now = time.Now
|
||||
25
_examples/auth/jwt/tutorial/util/password.go
Normal file
25
_examples/auth/jwt/tutorial/util/password.go
Normal file
@@ -0,0 +1,25 @@
|
||||
package util
|
||||
|
||||
import "golang.org/x/crypto/bcrypt"
|
||||
|
||||
// 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
|
||||
}
|
||||
23
_examples/auth/jwt/tutorial/util/uuid.go
Normal file
23
_examples/auth/jwt/tutorial/util/uuid.go
Normal file
@@ -0,0 +1,23 @@
|
||||
package util
|
||||
|
||||
import "github.com/google/uuid"
|
||||
|
||||
// MustGenerateUUID returns a new v4 UUID or panics.
|
||||
func MustGenerateUUID() string {
|
||||
id, err := GenerateUUID()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
return id
|
||||
}
|
||||
|
||||
// GenerateUUID returns a new v4 UUID.
|
||||
func GenerateUUID() (string, error) {
|
||||
id, err := uuid.NewRandom()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
return id.String(), nil
|
||||
}
|
||||
Reference in New Issue
Block a user