/
plugin.go
211 lines (187 loc) · 4.63 KB
/
plugin.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
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package google
import (
"aureole/internal/configs"
"aureole/internal/core"
"context"
_ "embed"
"errors"
"fmt"
"github.com/getkin/kin-openapi/openapi3"
"github.com/getkin/kin-openapi/openapi3gen"
"net/http"
"strconv"
"github.com/gofiber/fiber/v2"
"github.com/mitchellh/mapstructure"
"golang.org/x/oauth2"
"golang.org/x/oauth2/endpoints"
)
//go:embed meta.yaml
var rawMeta []byte
var meta core.Metadata
// init initializes package by register pluginCreator
func init() {
meta = core.AuthenticatorRepo.Register(rawMeta, Create)
}
type (
state struct {
State string `query:"state"`
}
code struct {
Code string `query:"code"`
}
GetAuthHandlerQuery struct {
State string `query:"state"`
Code string `query:"code"`
}
)
type google struct {
pluginAPI core.PluginAPI
rawConf configs.AuthPluginConfig
conf *config
provider *oauth2.Config
}
func (g *google) GetAuthHTTPMethod() string {
return http.MethodGet
}
func Create(conf configs.AuthPluginConfig) core.Authenticator {
return &google{rawConf: conf}
}
func (g *google) Init(api core.PluginAPI) (err error) {
g.pluginAPI = api
g.conf, err = initConfig(&g.rawConf.Config)
if err != nil {
return err
}
if err := initProvider(g); err != nil {
return err
}
return nil
}
func (google) GetMetadata() core.Metadata {
return meta
}
func (g *google) GetAuthHandler() core.AuthHandlerFunc {
return func(c fiber.Ctx) (*core.AuthResult, error) {
input := &GetAuthHandlerQuery{}
if err := c.QueryParser(input); err != nil {
return nil, err
}
// todo: save state and compare later #2
if input.State != core.State {
return nil, errors.New("invalid state")
}
if input.Code == "" {
return nil, errors.New("code not found")
}
var (
pluginId = fmt.Sprintf("%d", meta.PluginID)
email string
providerId string
)
jwtT, err := getJwt(g, input.Code)
if err != nil {
return nil, errors.New("error while exchange")
}
if err = g.pluginAPI.GetFromJWT(jwtT, core.Email, &email); err != nil {
return nil, errors.New("cannot get email from token")
}
if err = g.pluginAPI.GetFromJWT(jwtT, core.Sub, &providerId); err != nil {
return nil, errors.New("cannot get sub from token")
}
userData, err := jwtT.AsMap(context.Background())
if err != nil {
return nil, err
}
ok, err := g.pluginAPI.Filter(convertUserData(userData), g.pluginAPI.GetAppAuthFilter())
if err != nil {
return nil, err
} else if !ok {
return nil, errors.New("input data doesn't pass filters")
}
return &core.AuthResult{
User: &core.User{
Email: &email,
EmailVerified: true,
},
ImportedUser: &core.ImportedUser{
ProviderId: providerId,
PluginID: pluginId,
ProviderName: meta.ShortName,
Additional: userData,
},
Cred: &core.Credential{
Name: core.Email,
Value: email,
},
Provider: meta.ShortName,
}, nil
}
}
func (g *google) GetOAS3AuthRequestBody() *openapi3.RequestBody {
return nil
}
func (g *google) GetOAS3AuthParameters() openapi3.Parameters {
stateSchema, _ := openapi3gen.NewSchemaRefForValue(state{}, nil)
codeSchema, _ := openapi3gen.NewSchemaRefForValue(code{}, nil)
return openapi3.Parameters{
{
Value: &openapi3.Parameter{
Name: "State",
In: "query",
Required: true,
Schema: stateSchema,
},
},
{
Value: &openapi3.Parameter{
Name: "Code",
In: "query",
Required: true,
Schema: codeSchema,
},
},
}
}
func initConfig(conf *configs.RawConfig) (*config, error) {
PluginConf := &config{}
if err := mapstructure.Decode(conf, PluginConf); err != nil {
return nil, err
}
PluginConf.setDefaults()
return PluginConf, nil
}
func initProvider(g *google) error {
redirectUri := g.pluginAPI.GetAppUrl()
redirectUri.Path = g.pluginAPI.GetAuthRoute(meta.ShortName)
g.provider = &oauth2.Config{
ClientID: g.conf.ClientId,
ClientSecret: g.conf.ClientSecret,
Endpoint: endpoints.Google,
RedirectURL: redirectUri.String(),
Scopes: g.conf.Scopes,
}
return nil
}
func (g *google) GetCustomAppRoutes() []*core.Route {
return []*core.Route{
{
Method: http.MethodGet,
Path: core.GetOAuthPathPostfix(),
Handler: getAuthCode(g),
OAS3Operation: assembleOAS3Operation(),
},
}
}
func assembleOAS3Operation() *openapi3.Operation {
description := "Redirect"
return &openapi3.Operation{
OperationID: meta.ShortName,
Description: meta.DisplayName,
Tags: []string{fmt.Sprintf("auth by %s", meta.DisplayName)},
Responses: map[string]*openapi3.ResponseRef{
strconv.Itoa(http.StatusFound): {
Value: core.AssembleOASRedirectResponse(&description),
},
},
}
}