-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget-auth-url.go
159 lines (137 loc) · 4.03 KB
/
get-auth-url.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
package google
import (
"context"
"fmt"
"github.com/tiny-systems/module/module"
"github.com/tiny-systems/module/registry"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
)
const (
GetAuthUrlComponent = "google_get_auth_url"
GetAuthUrlRequestPort = "request"
GetAuthUrlResponsePort = "response"
GetAuthUrlErrorPort = "error"
)
type GetAuthUrlInContext any
type GetAuthUrlInMessage struct {
Context GetAuthUrlInContext `json:"context" title:"Context" configurable:"true" propertyOrder:"1"`
Config ClientConfig `json:"config" required:"true" title:"Client credentials" propertyOrder:"2"`
AccessType string `json:"accessType" title:"Type of access" enum:"offline,online" enumTitles:"Offline,Online" required:"true" propertyOrder:"3"`
ApprovalForce bool `json:"approvalForce" title:"ApprovalForce" required:"true" propertyOrder:"4"`
}
type GetAuthUrlSettings struct {
EnableErrorPort bool `json:"enableErrorPort" required:"true" title:"Enable Error Port" description:"If request may fail, error port will emit an error message"`
}
type GetAuthUrlErrorMessage struct {
Request GetAuthUrlInMessage `json:"request"`
Error string `json:"error"`
}
type GetAuthUrlOutMessage struct {
Request GetAuthUrlInMessage `json:"request"`
AuthUrl string `json:"authUrl" format:"uri"`
}
type GetAuthUrl struct {
settings GetAuthUrlSettings
}
func (a *GetAuthUrl) GetInfo() module.ComponentInfo {
return module.ComponentInfo{
Name: GetAuthUrlComponent,
Description: "Get Auth URL",
Info: "Gets Auth URL which later may be used fot auth redirect",
Tags: []string{"google", "auth"},
}
}
func (a *GetAuthUrl) Handle(ctx context.Context, output module.Handler, port string, msg interface{}) error {
if port == module.SettingsPort {
in, ok := msg.(GetAuthUrlSettings)
if !ok {
return fmt.Errorf("invalid settings")
}
a.settings = in
return nil
}
if port != GetAuthUrlRequestPort {
return fmt.Errorf("unknown port %s", port)
}
//
in, ok := msg.(GetAuthUrlInMessage)
if !ok {
return fmt.Errorf("invalid input message")
}
url, err := getAuthUrl(ctx, in)
if err != nil {
// check err port
if !a.settings.EnableErrorPort {
return err
}
return output(ctx, GetAuthUrlErrorPort, GetAuthUrlErrorMessage{
Request: in,
Error: err.Error(),
})
}
return output(ctx, GetAuthUrlResponsePort, GetAuthUrlOutMessage{
Request: in,
AuthUrl: url,
})
}
func getAuthUrl(_ context.Context, in GetAuthUrlInMessage) (string, error) {
config, err := google.ConfigFromJSON([]byte(in.Config.Credentials), in.Config.Scopes...)
if err != nil {
return "", fmt.Errorf("unable to parse client secret file to config: %v", err)
}
var opts []oauth2.AuthCodeOption
if in.ApprovalForce {
opts = append(opts, oauth2.ApprovalForce)
}
if in.AccessType == "online" {
opts = append(opts, oauth2.AccessTypeOnline)
} else {
opts = append(opts, oauth2.AccessTypeOffline)
}
return config.AuthCodeURL("state-token", opts...), nil
}
func (a *GetAuthUrl) Ports() []module.Port {
ports := []module.Port{
{
Source: true,
Name: GetAuthUrlRequestPort,
Label: "Request",
Position: module.Left,
Configuration: GetAuthUrlInMessage{
AccessType: "offline",
ApprovalForce: true,
},
},
{
Name: module.SettingsPort,
Label: "Settings",
Configuration: GetAuthUrlSettings{},
Source: true,
},
{
Source: false,
Name: GetAuthUrlResponsePort,
Label: "Response",
Position: module.Right,
Configuration: GetAuthUrlOutMessage{},
},
}
if !a.settings.EnableErrorPort {
return ports
}
return append(ports, module.Port{
Position: module.Bottom,
Name: GetAuthUrlErrorPort,
Label: "Error",
Source: false,
Configuration: GetAuthUrlErrorMessage{},
})
}
func (a *GetAuthUrl) Instance() module.Component {
return &GetAuthUrl{}
}
var _ module.Component = (*GetAuthUrl)(nil)
func init() {
registry.Register(&GetAuthUrl{})
}