/
callback.go
57 lines (48 loc) · 1.62 KB
/
callback.go
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package controller
import (
"github.com/kataras/iris/v12"
"github.com/mehulgohil/go-bffauth.git/authenticator"
"github.com/mehulgohil/go-bffauth.git/config"
"github.com/mehulgohil/go-bffauth.git/interfaces"
"net/http"
"time"
)
type CallbackHandler struct {
Auth *authenticator.Authenticator
RedisClient interfaces.IRedisLayer
}
func (c *CallbackHandler) Callback(ctx iris.Context) {
if ctx.URLParam("state") != state {
ctx.StopWithJSON(http.StatusBadRequest, "Invalid state parameter.")
return
}
// Exchange an authorization code for a token.
token, err := c.Auth.Exchange(ctx.Request().Context(), ctx.URLParam("code"))
if err != nil {
ctx.StopWithJSON(http.StatusUnauthorized, "Failed to convert an authorization code into a token.")
return
}
idToken, err := c.Auth.VerifyIDToken(ctx.Request().Context(), token)
if err != nil {
ctx.StopWithJSON(http.StatusInternalServerError, "Failed to verify ID Token.")
return
}
var profile map[string]interface{}
if err := idToken.Claims(&profile); err != nil {
ctx.StopWithError(http.StatusInternalServerError, err)
return
}
err = c.RedisClient.SetKeyValue(profile["email"].(string)+"_token", token.AccessToken, 24*time.Hour)
if err != nil {
ctx.StopWithError(http.StatusInternalServerError, err)
return
}
err = c.RedisClient.HSetKeyValue(profile["email"].(string)+"_profile", profile, 24*time.Hour)
if err != nil {
ctx.StopWithError(http.StatusInternalServerError, err)
return
}
ctx.SetCookieKV("logged_id_email", profile["email"].(string))
// Redirect to logged in page.
ctx.Redirect(config.EnvVariables.FrontendURL, http.StatusTemporaryRedirect)
}