DEVELOPMENT ENVIRONMENT

~liljamo/tixe

ref: 960570f130cd5d9e4f2376edd399874c6740f650 tixe/handlers/authlogin.go -rw-r--r-- 784 bytes
960570f1Jonni Liljamo fix: make placeholder lighter in tag input 1 year, 2 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package handlers

import (
	"crypto/rand"
	"encoding/base64"
	"net/http"
	"tixe/auth"

	"github.com/gin-contrib/sessions"
	"github.com/gin-gonic/gin"
)

func AuthLogin(auth *auth.Auth) gin.HandlerFunc {
	return func(c *gin.Context) {
		state, err := randomState()
		if err != nil {
			c.String(http.StatusInternalServerError, err.Error())
			return
		}

		session := sessions.Default(c)
		session.Set("state", state)
		if err := session.Save(); err != nil {
			c.String(http.StatusInternalServerError, err.Error())
			return
		}

		c.Redirect(http.StatusTemporaryRedirect, auth.AuthCodeURL(state))
	}
}

func randomState() (string, error) {
	buf := make([]byte, 32)
	_, err := rand.Read(buf)
	if err != nil {
		return "", nil
	}

	return base64.StdEncoding.EncodeToString(buf), nil
}