Moved config and finished JWT module

This commit is contained in:
2025-02-10 22:10:03 +11:00
parent 04049bb73a
commit e73805a02d
13 changed files with 237 additions and 69 deletions

18
cookies/delete.go Normal file
View File

@@ -0,0 +1,18 @@
package cookies
import (
"net/http"
"time"
)
// Tell the browser to delete the cookie matching the name provided
// Path must match the original set cookie for it to delete
func DeleteCookie(w http.ResponseWriter, name string, path string) {
http.SetCookie(w, &http.Cookie{
Name: name,
Value: "",
Path: path,
Expires: time.Unix(0, 0), // Expire in the past
MaxAge: -1, // Immediately expire
})
}

View File

@@ -3,7 +3,6 @@ package cookies
import (
"net/http"
"net/url"
"time"
)
// Check the value of "pagefrom" cookie, delete the cookie, and return the value
@@ -13,8 +12,7 @@ func CheckPageFrom(w http.ResponseWriter, r *http.Request) string {
return "/"
}
pageFrom := pageFromCookie.Value
deleteCookie := &http.Cookie{Name: "pagefrom", Value: "", Expires: time.Unix(0, 0)}
http.SetCookie(w, deleteCookie)
DeleteCookie(w, pageFromCookie.Name, pageFromCookie.Path)
return pageFrom
}
@@ -35,6 +33,6 @@ func SetPageFrom(w http.ResponseWriter, r *http.Request, trustedHost string) {
} else {
pageFrom = parsedURL.Path
}
pageFromCookie := &http.Cookie{Name: "pagefrom", Value: pageFrom}
pageFromCookie := &http.Cookie{Name: "pagefrom", Value: pageFrom, Path: "/"}
http.SetCookie(w, pageFromCookie)
}

48
cookies/tokens.go Normal file
View File

@@ -0,0 +1,48 @@
package cookies
import (
"net/http"
"projectreshoot/config"
"time"
)
// Get the value of the access and refresh tokens
func GetTokens(
w http.ResponseWriter,
r *http.Request,
) (acc string, ref string) {
accCookie, accErr := r.Cookie("access")
refCookie, refErr := r.Cookie("refresh")
var (
accStr string = ""
refStr string = ""
)
if accErr == nil {
accStr = accCookie.Value
}
if refErr == nil {
refStr = refCookie.Value
}
return accStr, refStr
}
// Set a token with the provided details
func SetToken(
w http.ResponseWriter,
r *http.Request,
config *config.Config,
token string,
scope string,
exp int64,
) {
tokenCookie := &http.Cookie{
Name: scope,
Value: token,
Path: "/",
Expires: time.Unix(exp, 0),
HttpOnly: true,
SameSite: http.SameSiteLaxMode,
Secure: config.SSL,
}
http.SetCookie(w, tokenCookie)
}