15
"xelbot.com/reprogl/container"
16
"xelbot.com/reprogl/models"
17
"xelbot.com/reprogl/models/repositories"
18
"xelbot.com/reprogl/security"
19
"xelbot.com/reprogl/session"
22
func pageOrRedirect(pageString string) (int, bool) {
25
if pageString == "1" {
27
} else if pageString == "" {
30
page, _ = strconv.Atoi(pageString)
36
func doESI(w http.ResponseWriter) {
37
w.Header().Set("Surrogate-Control", "content=\"ESI/1.0\"")
40
func cacheControl(w http.ResponseWriter, age int) {
41
w.Header().Set("Cache-Control", fmt.Sprintf("max-age=%d", age))
44
func setExpires(w http.ResponseWriter, t time.Time) {
46
w.Header().Set("Expires", fmt.Sprintf("%s GMT", t.Format("Mon, 02 Jan 2006 15:04:05")))
49
func jsonResponse(w http.ResponseWriter, statusCode int, data any) {
50
w.Header().Set("Content-Type", "application/json")
51
if statusCode != http.StatusOK {
52
w.WriteHeader(statusCode)
55
jsonResult, _ := json.Marshal(data)
59
func generateRandomToken() string {
60
nonce := make([]byte, 18)
61
_, err := rand.Read(nonce)
66
return base64.URLEncoding.EncodeToString(nonce)
69
func authSuccess(user *models.LoggedUser, app *container.Application, ip string, ctx context.Context) {
70
session.SetIdentity(ctx, security.CreateIdentity(user))
72
repo := repositories.UserRepository{DB: app.DB}
73
if err := repo.SaveLoginEvent(user.ID, ip); err != nil {
78
func saveLoginReferer(w http.ResponseWriter, r *http.Request) {
79
if _, errNoCookie := r.Cookie(session.RefererCookie); errNoCookie == nil {
83
host := strings.ReplaceAll(container.GetConfig().Host, ".", "\\.")
84
matches := regexp.MustCompile(`^https?:\/\/` + host + `(.*)$`).FindStringSubmatch(r.Referer())
85
if matches != nil && matches[1] != "/login" && !strings.HasPrefix(matches[1], "/oauth") {
86
session.WriteSessionCookie(w, session.RefererCookie, matches[1], "/")
90
func popLoginReferer(w http.ResponseWriter, r *http.Request) (redirectUrl string, found bool) {
91
if cookie, errNoCookie := r.Cookie(session.RefererCookie); errNoCookie == nil {
92
redirectUrl = cookie.Value
95
deleteRefererCookie(w)
101
func deleteRefererCookie(w http.ResponseWriter) {
102
session.DeleteCookie(w, session.RefererCookie, "/")