add jwt support

master
Joshua Herring 3 weeks ago
parent 53e4f39547
commit 1bf4d2420b

@ -0,0 +1,18 @@
package dbi
import (
"time"
"github.com/golang-jwt/jwt/v5"
)
func GenJWT(user User) (string, error) {
now := time.Now()
claims := jwt.MapClaims{
"sub": user.Username,
"iat": now.Unix(),
"exp": now.Add(time.Hour).Unix(),
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString([]byte(user.APIKey))
}

@ -4,6 +4,7 @@ go 1.26
require ( require (
github.com/dustin/go-humanize v1.0.1 // indirect github.com/dustin/go-humanize v1.0.1 // indirect
github.com/golang-jwt/jwt/v5 v5.3.1 // indirect
github.com/google/uuid v1.6.0 // indirect github.com/google/uuid v1.6.0 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-isatty v0.0.20 // indirect
github.com/ncruces/go-strftime v1.0.0 // indirect github.com/ncruces/go-strftime v1.0.0 // indirect

@ -59,7 +59,13 @@ func handleLoginPost(w http.ResponseWriter, r *http.Request) {
return return
} }
writeHTML(w, pages.MainFormPage) token, err := dbi.GenJWT(user)
if err != nil {
fail()
return
}
writeHTML(w, pages.MainFormPage(token))
} }
func main() { func main() {

@ -1,12 +1,16 @@
package pages package pages
const MainFormPage = `<!DOCTYPE html> import "fmt"
func MainFormPage(jwtToken string) string {
return fmt.Sprintf(`<!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Faculty Activity Report</title> <title>Faculty Activity Report</title>
<link rel="stylesheet" href="https://unpkg.com/rivet-core@2/css/rivet.min.css"> <link rel="stylesheet" href="https://unpkg.com/rivet-core@2/css/rivet.min.css">
<script>var JWT = "%s";</script>
</head> </head>
<body> <body>
<header class="rvt-header-wrapper"> <header class="rvt-header-wrapper">
@ -293,4 +297,5 @@ const MainFormPage = `<!DOCTYPE html>
}); });
</script> </script>
</body> </body>
</html>` </html>`, jwtToken)
}

Loading…
Cancel
Save