Add working JWT tokens

This commit is contained in:
Andrew-71 2024-10-12 10:22:05 +03:00
parent 1f8393a985
commit c34f789567
2 changed files with 12 additions and 4 deletions

View file

@ -9,4 +9,10 @@ with(out) blazingly fast cloud-native web3 memory-safe blockchain reactive AI
This should be done by **October 17th 2024**. Or, at the very least,
in a state that proves I am competent Go developer.
Note: **JSON** is used for storage at proof-of-concept stage for ease of use
Note: **JSON** is used for storage at proof-of-concept stage for ease of use
## Current functionality
* `POST /register` - register a user with Basic Auth
* `POST /login` - get a JWT token by Basic Auth
* `GET /public-key` - get PEM-encoded public HS256 key

8
jwt.go
View file

@ -10,6 +10,7 @@ import (
"log/slog"
"net/http"
"os"
"time"
"github.com/golang-jwt/jwt/v5"
)
@ -18,7 +19,6 @@ var KeyFile = "key"
var (
key *ecdsa.PrivateKey
// t *jwt.Token
)
// LoadKey attempts to load a private key from KeyFile.
@ -76,8 +76,10 @@ func CreateJWT(usr User) (string, error) {
t := jwt.NewWithClaims(jwt.SigningMethodES256,
jwt.MapClaims{
"iss": "pye",
"sub": "john",
"foo": 2,
"uid": usr.Uuid,
"sub": usr.Email,
"iat": time.Now(),
"exp": time.Now().Add(time.Hour * 24 * 7),
})
s, err := t.SignedString(key)
if err != nil {