-
-
Notifications
You must be signed in to change notification settings - Fork 121
/
auth_social_login.go
112 lines (95 loc) · 3.25 KB
/
auth_social_login.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package handler
import (
"github.com/ArtalkJS/Artalk/internal/auth"
"github.com/ArtalkJS/Artalk/internal/auth/gothic_fiber"
"github.com/ArtalkJS/Artalk/internal/core"
"github.com/ArtalkJS/Artalk/internal/log"
"github.com/ArtalkJS/Artalk/server/common"
"github.com/gofiber/fiber/v2"
"github.com/markbates/goth"
)
func SocialLoginGuard(app *core.App, handler fiber.Handler) fiber.Handler {
return func(c *fiber.Ctx) error {
if !app.Conf().Auth.Enabled {
return common.RespError(c, 404, "Auth Api disabled")
}
return handler(c)
}
}
type ResponseConfAuthProviders struct {
Providers []auth.AuthProviderInfo `json:"providers" validator:"required"`
Anonymous bool `json:"anonymous" validator:"required"`
}
// @Id GetSocialLoginProviders
// @Summary Get Social Login Providers
// @Description Get social login providers
// @Tags System
// @Produce json
// @Success 200 {object} ResponseConfAuthProviders
// @Success 404 {object} Map{msg=string}
// @Router /conf/auth/providers [get]
func AuthSocialLogin(app *core.App, router fiber.Router) {
// Load providers
var providers []goth.Provider
loadProviders := func() {
providers = auth.GetProviders(app.Conf())
goth.ClearProviders()
goth.UseProviders(providers...)
}
loadProviders()
app.OnConfUpdated().Add(func(e *core.ConfUpdatedEvent) error {
loadProviders()
return nil
})
// Endpoints
router.Get("/conf/auth/providers", SocialLoginGuard(app, func(c *fiber.Ctx) error {
return common.RespData(c, ResponseConfAuthProviders{
Providers: auth.GetProviderInfo(app.Conf(), providers),
Anonymous: app.Conf().Auth.Anonymous,
})
}))
router.Get("/auth/:provider", SocialLoginGuard(app, func(c *fiber.Ctx) error {
return gothic_fiber.BeginAuthHandler(c)
}))
router.Get("/auth/:provider/callback", SocialLoginGuard(app, func(c *fiber.Ctx) error {
provider, err := gothic_fiber.GetProviderName(c)
if err != nil {
log.Error("[SocialLogin] ", err)
return common.RespError(c, 500, "Field to get provider name")
}
// Fetch user
gothUser, err := gothic_fiber.CompleteUserAuth(c)
if err != nil {
log.Error("[SocialLogin] ", err)
return common.RespError(c, 500, "Field to complete user auth")
}
// Convert to social user
socialUser := auth.GetSocialUser(gothUser)
log.Debug("[SocialLogin] ", socialUser)
// Find auth identity
authIdentity := app.Dao().FindAuthIdentityByRemoteUID(provider, socialUser.RemoteUID)
// No auth identity record, register user
if authIdentity.IsEmpty() {
authIdentity, err = auth.RegisterSocialUser(app.Dao(), socialUser)
if err != nil {
log.Error("[SocialLogin] ", err)
return common.RespError(c, 500, "Failed to register user")
}
}
if authIdentity.UserID == 0 {
return common.RespError(c, 500, "Auth Identity user_id invalid")
}
// Find user perform login
user := app.Dao().FindUserByID(authIdentity.UserID)
if user.IsEmpty() {
return common.RespError(c, 500, "Failed to find user")
}
// Get user token
jwtToken, err := common.LoginGetUserToken(user, app.Conf().AppKey, app.Conf().LoginTimeout)
if err != nil {
return common.RespError(c, 500, err.Error())
}
// Render response
return auth.ResponseCallbackPage(c, jwtToken)
}))
}