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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
package controller
import (
"github.com/miniflux/miniflux/config"
"github.com/miniflux/miniflux/logger"
"github.com/miniflux/miniflux/model"
"github.com/miniflux/miniflux/server/cookie"
"github.com/miniflux/miniflux/server/core"
"github.com/miniflux/miniflux/server/oauth2"
"github.com/tomasen/realip"
)
// OAuth2Redirect redirects the user to the consent page to ask for permission.
func (c *Controller) OAuth2Redirect(ctx *core.Context, request *core.Request, response *core.Response) {
provider := request.StringParam("provider", "")
if provider == "" {
logger.Error("[OAuth2] Invalid or missing provider: %s", provider)
response.Redirect(ctx.Route("login"))
return
}
authProvider, err := getOAuth2Manager(c.cfg).Provider(provider)
if err != nil {
logger.Error("[OAuth2] %v", err)
response.Redirect(ctx.Route("login"))
return
}
response.Redirect(authProvider.GetRedirectURL(ctx.GenerateOAuth2State()))
}
// OAuth2Callback receives the authorization code and create a new session.
func (c *Controller) OAuth2Callback(ctx *core.Context, request *core.Request, response *core.Response) {
provider := request.StringParam("provider", "")
if provider == "" {
logger.Error("[OAuth2] Invalid or missing provider")
response.Redirect(ctx.Route("login"))
return
}
code := request.QueryStringParam("code", "")
if code == "" {
logger.Error("[OAuth2] No code received on callback")
response.Redirect(ctx.Route("login"))
return
}
state := request.QueryStringParam("state", "")
if state == "" || state != ctx.OAuth2State() {
logger.Error(`[OAuth2] Invalid state value: got "%s" instead of "%s"`, state, ctx.OAuth2State())
response.Redirect(ctx.Route("login"))
return
}
authProvider, err := getOAuth2Manager(c.cfg).Provider(provider)
if err != nil {
logger.Error("[OAuth2] %v", err)
response.Redirect(ctx.Route("login"))
return
}
profile, err := authProvider.GetProfile(code)
if err != nil {
logger.Error("[OAuth2] %v", err)
response.Redirect(ctx.Route("login"))
return
}
if ctx.IsAuthenticated() {
user := ctx.LoggedUser()
if err := c.store.UpdateExtraField(user.ID, profile.Key, profile.ID); err != nil {
response.HTML().ServerError(err)
return
}
ctx.SetFlashMessage(ctx.Translate("Your external account is now linked !"))
response.Redirect(ctx.Route("settings"))
return
}
user, err := c.store.UserByExtraField(profile.Key, profile.ID)
if err != nil {
response.HTML().ServerError(err)
return
}
if user == nil {
if c.cfg.GetInt("OAUTH2_USER_CREATION", 0) == 0 {
response.HTML().Forbidden()
return
}
user = model.NewUser()
user.Username = profile.Username
user.IsAdmin = false
user.Extra[profile.Key] = profile.ID
if err := c.store.CreateUser(user); err != nil {
response.HTML().ServerError(err)
return
}
}
sessionToken, err := c.store.CreateUserSession(
user.Username,
request.Request().UserAgent(),
realip.RealIP(request.Request()),
)
if err != nil {
response.HTML().ServerError(err)
return
}
logger.Info("[Controller:OAuth2Callback] username=%s just logged in", user.Username)
response.SetCookie(cookie.New(cookie.CookieUserSessionID, sessionToken, request.IsHTTPS()))
response.Redirect(ctx.Route("unread"))
}
// OAuth2Unlink unlink an account from the external provider.
func (c *Controller) OAuth2Unlink(ctx *core.Context, request *core.Request, response *core.Response) {
provider := request.StringParam("provider", "")
if provider == "" {
logger.Info("[OAuth2] Invalid or missing provider")
response.Redirect(ctx.Route("login"))
return
}
authProvider, err := getOAuth2Manager(c.cfg).Provider(provider)
if err != nil {
logger.Error("[OAuth2] %v", err)
response.Redirect(ctx.Route("settings"))
return
}
user := ctx.LoggedUser()
if err := c.store.RemoveExtraField(user.ID, authProvider.GetUserExtraKey()); err != nil {
response.HTML().ServerError(err)
return
}
response.Redirect(ctx.Route("settings"))
return
}
func getOAuth2Manager(cfg *config.Config) *oauth2.Manager {
return oauth2.NewManager(
cfg.Get("OAUTH2_CLIENT_ID", ""),
cfg.Get("OAUTH2_CLIENT_SECRET", ""),
cfg.Get("OAUTH2_REDIRECT_URL", ""),
)
}
|