/
wechat_redirect_uri_middleware.go
117 lines (101 loc) · 3.08 KB
/
wechat_redirect_uri_middleware.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
113
114
115
116
117
package webapp
import (
"net/http"
"net/url"
"github.com/authgear/authgear-server/pkg/api/apierrors"
"github.com/authgear/authgear-server/pkg/lib/config"
"github.com/authgear/authgear-server/pkg/util/httputil"
"github.com/authgear/authgear-server/pkg/util/wechat"
)
// WeChatRedirectURICookieDef is a HTTP session cookie.
var WeChatRedirectURICookieDef = &httputil.CookieDef{
NameSuffix: "wechat_redirect_uri",
Path: "/",
SameSite: http.SameSiteNoneMode,
}
// PlatformCookieDef is a HTTP session cookie.
var PlatformCookieDef = &httputil.CookieDef{
NameSuffix: "platform",
Path: "/",
SameSite: http.SameSiteNoneMode,
}
// WeChatRedirectURIMiddleware validates x_wechat_redirect_uri and stores it in context.
// Ideally we should store x_wechat_redirect_uri in web app session.
// But we can link wechat in settings page so that is not possible at the moment.
type WeChatRedirectURIMiddleware struct {
Cookies CookieManager
IdentityConfig *config.IdentityConfig
}
func (m *WeChatRedirectURIMiddleware) Handle(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
q := r.URL.Query()
if m.isWechatEnabled() {
m.populateWechatRedirectURI(w, r, q)
}
// Repeat the steps for platform
platform := q.Get("x_platform")
if platform != "" {
cookie := m.Cookies.ValueCookie(PlatformCookieDef, platform)
httputil.UpdateCookie(w, cookie)
}
if platform == "" {
cookie, err := m.Cookies.GetCookie(r, PlatformCookieDef)
if err == nil {
platform = cookie.Value
}
}
if platform != "" {
ctx := wechat.WithPlatform(r.Context(), platform)
r = r.WithContext(ctx)
}
next.ServeHTTP(w, r)
})
}
func (m *WeChatRedirectURIMiddleware) isWechatEnabled() bool {
for _, providerConfig := range m.IdentityConfig.OAuth.Providers {
if providerConfig.Type == config.OAuthSSOProviderTypeWechat {
return true
}
}
return false
}
func (m *WeChatRedirectURIMiddleware) populateWechatRedirectURI(
w http.ResponseWriter,
r *http.Request,
q url.Values,
) {
weChatRedirectURI := q.Get("x_wechat_redirect_uri")
if weChatRedirectURI != "" {
// Validate x_wechat_redirect_uri
valid := false
for _, providerConfig := range m.IdentityConfig.OAuth.Providers {
if providerConfig.Type == config.OAuthSSOProviderTypeWechat {
for _, allowed := range providerConfig.WeChatRedirectURIs {
if weChatRedirectURI == allowed {
valid = true
}
}
}
}
if !valid {
panic(apierrors.NewInvalid("wechat redirect URI is not allowed"))
}
}
// Persist weChatRedirectURI.
if weChatRedirectURI != "" {
cookie := m.Cookies.ValueCookie(WeChatRedirectURICookieDef, weChatRedirectURI)
httputil.UpdateCookie(w, cookie)
}
// Restore weChatRedirectURI from cookie
if weChatRedirectURI == "" {
cookie, err := m.Cookies.GetCookie(r, WeChatRedirectURICookieDef)
if err == nil {
weChatRedirectURI = cookie.Value
}
}
// Restore weChatRedirectURI into the request context.
if weChatRedirectURI != "" {
ctx := wechat.WithWeChatRedirectURI(r.Context(), weChatRedirectURI)
r = r.WithContext(ctx)
}
}