Producing jwt tokens

This commit is contained in:
2026-03-26 23:30:59 -04:00
parent a98f92d6ab
commit 9f21716805
3 changed files with 22 additions and 4 deletions

View File

@@ -1,7 +1,3 @@
/*
* This API call authenticates a user
*/
package main package main
import ( import (
@@ -23,11 +19,21 @@ type AuthenticateRequest struct {
func authenticateHandler(w http.ResponseWriter, r *http.Request) { func authenticateHandler(w http.ResponseWriter, r *http.Request) {
/*
var hashText string
var salt []byte
var err error
var checkExisting pgx.Row
var req CreateIdentityRequest
var accountid string
*/
var authenticaterequest AuthenticateRequest var authenticaterequest AuthenticateRequest
var err error var err error
var checkExisting pgx.Row var checkExisting pgx.Row
var hashText string var hashText string
var ok bool var ok bool
var token string
secret := []byte("super-secret-key")
response := map[string]interface{}{} response := map[string]interface{}{}
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
@@ -90,8 +96,17 @@ func authenticateHandler(w http.ResponseWriter, r *http.Request) {
goto ExitAPICall goto ExitAPICall
} }
token, err = createJWT(secret, fmt.Sprintf("%d", authenticaterequest.Accountid), authenticaterequest.Username, "user")
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
response["status"] = "error"
response["message"] = "Failed to create JWT"
goto ExitAPICall
}
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
response["status"] = "success" response["status"] = "success"
response["token"] = token
ExitAPICall: ExitAPICall:
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
response["timestamp"] = fmt.Sprintf("%d", time.Now().Unix()) response["timestamp"] = fmt.Sprintf("%d", time.Now().Unix())

1
go.mod
View File

@@ -8,6 +8,7 @@ require (
) )
require ( require (
github.com/golang-jwt/jwt/v5 v5.3.1 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect github.com/jackc/puddle/v2 v2.2.2 // indirect

2
go.sum
View File

@@ -1,6 +1,8 @@
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY=
github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM= github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo= github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=