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

@ -10,3 +10,9 @@ This should be done by **October 17th 2024**. Or, at the very least,
in a state that proves I am competent Go developer. 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" "log/slog"
"net/http" "net/http"
"os" "os"
"time"
"github.com/golang-jwt/jwt/v5" "github.com/golang-jwt/jwt/v5"
) )
@ -18,7 +19,6 @@ var KeyFile = "key"
var ( var (
key *ecdsa.PrivateKey key *ecdsa.PrivateKey
// t *jwt.Token
) )
// LoadKey attempts to load a private key from KeyFile. // LoadKey attempts to load a private key from KeyFile.
@ -76,8 +76,10 @@ func CreateJWT(usr User) (string, error) {
t := jwt.NewWithClaims(jwt.SigningMethodES256, t := jwt.NewWithClaims(jwt.SigningMethodES256,
jwt.MapClaims{ jwt.MapClaims{
"iss": "pye", "iss": "pye",
"sub": "john", "uid": usr.Uuid,
"foo": 2, "sub": usr.Email,
"iat": time.Now(),
"exp": time.Now().Add(time.Hour * 24 * 7),
}) })
s, err := t.SignedString(key) s, err := t.SignedString(key)
if err != nil { if err != nil {