-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexchange-auth-code.go
153 lines (129 loc) · 3.99 KB
/
exchange-auth-code.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
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 (
ExchangeAutCodeComponent = "google_exchange_auth_code"
ExchangeAuthCodeRequestPort = "request"
ExchangeAuthCodeResponsePort = "response"
ExchangeAuthCodeErrorPort = "error"
)
type ExchangeAuthCodeInContext any
type ExchangeAuthCodeInMessage struct {
Context ExchangeAuthCodeInContext `json:"context" title:"Context" configurable:"true" propertyOrder:"1"`
Config ClientConfig `json:"config" title:"Config" required:"true" description:"Client Config" propertyOrder:"2"`
AuthCode string `json:"authCode" required:"true" title:"Authorisation code" propertyOrder:"3"`
}
type ExchangeAuthCodeSettings struct {
EnableErrorPort bool `json:"enableErrorPort" required:"true" title:"Enable Error Port" description:"If request may fail, error port will emit an error message"`
}
type ExchangeAuthCodeOutMessage struct {
Context ExchangeAuthCodeInContext `json:"context" title:"Context" propertyOrder:"1"`
Token Token `json:"token" propertyOrder:"2"`
}
type ExchangeAuthCodeError struct {
Request ExchangeAuthCodeInMessage `json:"request"`
Error string `json:"error"`
}
///
type ExchangeAuthCode struct {
settings ExchangeAuthCodeSettings
}
func (a *ExchangeAuthCode) GetInfo() module.ComponentInfo {
return module.ComponentInfo{
Name: ExchangeAutCodeComponent,
Description: "Exchange Auth Code",
Info: "Exchanges Auth code to Auth token",
Tags: []string{"google", "auth"},
}
}
func (a *ExchangeAuthCode) exchange(ctx context.Context, in ExchangeAuthCodeInMessage) (*oauth2.Token, error) {
config, err := google.ConfigFromJSON([]byte(in.Config.Credentials), in.Config.Scopes...)
if err != nil {
return nil, fmt.Errorf("unable to parse client secret file to config: %v", err)
}
return config.Exchange(ctx, in.AuthCode)
}
func (a *ExchangeAuthCode) Handle(ctx context.Context, output module.Handler, port string, msg interface{}) error {
if port == module.SettingsPort {
in, ok := msg.(ExchangeAuthCodeSettings)
if !ok {
return fmt.Errorf("invalid settings")
}
a.settings = in
return nil
}
if port != ExchangeAuthCodeRequestPort {
return fmt.Errorf("unknown port %s", port)
}
in, ok := msg.(ExchangeAuthCodeInMessage)
if !ok {
return fmt.Errorf("invalid input message")
}
token, err := a.exchange(ctx, in)
if err != nil {
// check err port
if !a.settings.EnableErrorPort {
return err
}
return output(ctx, ExchangeAuthCodeErrorPort, ExchangeAuthCodeError{
Request: in,
Error: err.Error(),
})
}
return output(ctx, ExchangeAuthCodeResponsePort, ExchangeAuthCodeOutMessage{
Context: in.Context,
Token: Token{
AccessToken: token.AccessToken,
RefreshToken: token.RefreshToken,
TokenType: token.TokenType,
Expiry: token.Expiry,
},
})
}
func (a *ExchangeAuthCode) Ports() []module.Port {
ports := []module.Port{
{
Name: module.SettingsPort,
Label: "Settings",
Configuration: ExchangeAuthCodeSettings{},
Source: true,
},
{
Source: true,
Name: ExchangeAuthCodeRequestPort,
Label: "Request",
Position: module.Left,
Configuration: ExchangeAuthCodeInMessage{},
},
{
Source: false,
Name: ExchangeAuthCodeResponsePort,
Label: "Response",
Position: module.Right,
Configuration: ExchangeAuthCodeOutMessage{},
},
}
if !a.settings.EnableErrorPort {
return ports
}
return append(ports, module.Port{
Position: module.Bottom,
Name: "error",
Label: "Error",
Source: false,
Configuration: ExchangeAuthCodeError{},
})
}
func (a *ExchangeAuthCode) Instance() module.Component {
return &ExchangeAuthCode{}
}
var _ module.Component = (*ExchangeAuthCode)(nil)
func init() {
registry.Register(&ExchangeAuthCode{})
}