Compare commits

..

No commits in common. "9a1f4a6fcf4d020217f34ec6442697ee09cc6921" and "cb75af9997cd4edec40a1f92db668e8a55309b44" have entirely different histories.

2 changed files with 24 additions and 19 deletions

View file

@ -1,7 +1,6 @@
package main package main
import ( import (
"compress/flate"
"fmt" "fmt"
_ "github.com/lib/pq" _ "github.com/lib/pq"
"log" "log"
@ -62,13 +61,11 @@ func Routes() *chi.Mux {
MaxAge: 360, MaxAge: 360,
}) })
compressor := middleware.NewCompressor(flate.DefaultCompression)
router.Use( router.Use(
compressor.Handler,
cors.Handler, cors.Handler,
render.SetContentType(render.ContentTypeJSON), render.SetContentType(render.ContentTypeJSON),
middleware.Logger, middleware.Logger,
middleware.DefaultCompress,
middleware.RedirectSlashes, middleware.RedirectSlashes,
middleware.Recoverer, middleware.Recoverer,
) )

View file

@ -8,6 +8,7 @@ import (
"git.minhas.io/asara/sudoscientist-go-backend/packages/middleware" "git.minhas.io/asara/sudoscientist-go-backend/packages/middleware"
"git.minhas.io/asara/sudoscientist-go-backend/packages/users" "git.minhas.io/asara/sudoscientist-go-backend/packages/users"
"github.com/badoux/checkmail" "github.com/badoux/checkmail"
"github.com/dgrijalva/jwt-go"
"github.com/go-chi/chi" "github.com/go-chi/chi"
"github.com/go-chi/jwtauth" "github.com/go-chi/jwtauth"
"github.com/go-chi/render" "github.com/go-chi/render"
@ -65,6 +66,7 @@ type Claims struct {
Username string `json:"username", db:"username"` Username string `json:"username", db:"username"`
Admin bool `json:"admin", db:"admin"` Admin bool `json:"admin", db:"admin"`
Verified bool `json:"verified", db:"verified"` Verified bool `json:"verified", db:"verified"`
jwt.StandardClaims
} }
type JWT struct { type JWT struct {
@ -173,12 +175,14 @@ func register(w http.ResponseWriter, r *http.Request) {
} }
users.CreateProfile(creds.Username, creds.Email) users.CreateProfile(creds.Username, creds.Email)
expirationTime := time.Now().Add(24 * time.Hour) expirationTime := time.Now().Add(24 * time.Hour)
claims := map[string]interface{}{ claims := &Claims{
"username": creds.Username, Username: creds.Username,
"admin": false, Admin: false,
"verified": false, Verified: false,
StandardClaims: jwt.StandardClaims{
ExpiresAt: expirationTime.Unix(),
},
} }
jwtauth.SetExpiry(claims, expirationTime)
if PostalEnabled { if PostalEnabled {
_, emailToken, _ := EmailAuth.Encode(claims) _, emailToken, _ := EmailAuth.Encode(claims)
returnMessage, ok := sendEmailToken(w, emailToken, creds.Username, creds.Email) returnMessage, ok := sendEmailToken(w, emailToken, creds.Username, creds.Email)
@ -223,12 +227,14 @@ func signin(w http.ResponseWriter, r *http.Request) {
user_claims := &Claims{} user_claims := &Claims{}
user_claims_query := DB.QueryRow("SELECT username, admin, verified FROM users WHERE username=$1", creds.Username) user_claims_query := DB.QueryRow("SELECT username, admin, verified FROM users WHERE username=$1", creds.Username)
err = user_claims_query.Scan(&user_claims.Username, &user_claims.Admin, &user_claims.Verified) err = user_claims_query.Scan(&user_claims.Username, &user_claims.Admin, &user_claims.Verified)
claims := map[string]interface{}{ claims := &Claims{
"username": user_claims.Username, Username: user_claims.Username,
"admin": user_claims.Admin, Admin: user_claims.Admin,
"verified": user_claims.Verified, Verified: user_claims.Verified,
StandardClaims: jwt.StandardClaims{
ExpiresAt: expirationTime.Unix(),
},
} }
jwtauth.SetExpiry(claims, expirationTime)
_, tokenString, _ := TokenAuth.Encode(claims) _, tokenString, _ := TokenAuth.Encode(claims)
setCookies(w, tokenString, expirationTime) setCookies(w, tokenString, expirationTime)
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
@ -252,12 +258,14 @@ func refresh(w http.ResponseWriter, r *http.Request) {
render.JSON(w, r, returnMessage) render.JSON(w, r, returnMessage)
return return
} }
newClaims := map[string]interface{}{ newClaims := &Claims{
"username": user_claims.Username, Username: user_claims.Username,
"admin": user_claims.Admin, Admin: user_claims.Admin,
"verified": user_claims.Verified, Verified: user_claims.Verified,
StandardClaims: jwt.StandardClaims{
ExpiresAt: expirationTime.Unix(),
},
} }
jwtauth.SetExpiry(newClaims, expirationTime)
_, tokenString, _ := TokenAuth.Encode(newClaims) _, tokenString, _ := TokenAuth.Encode(newClaims)
setCookies(w, tokenString, expirationTime) setCookies(w, tokenString, expirationTime)
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)