-
Notifications
You must be signed in to change notification settings - Fork 329
/
commands.go
184 lines (152 loc) · 4.33 KB
/
commands.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
package commands
import (
"context"
"time"
"github.com/cortezaproject/corteza/server/auth/external"
"github.com/cortezaproject/corteza/server/pkg/auth"
"github.com/cortezaproject/corteza/server/pkg/cli"
"github.com/cortezaproject/corteza/server/pkg/logger"
"github.com/cortezaproject/corteza/server/pkg/options"
"github.com/cortezaproject/corteza/server/store"
"github.com/cortezaproject/corteza/server/system/service"
"github.com/cortezaproject/corteza/server/system/types"
"github.com/spf13/cobra"
)
type (
serviceInitializer interface {
InitServices(ctx context.Context) error
Activate(ctx context.Context) error
Options() *options.Options
}
)
func commandPreRunInitService(app serviceInitializer) func(*cobra.Command, []string) error {
return func(_ *cobra.Command, _ []string) error {
return app.InitServices(cli.Context())
}
}
func commandPreRunInitActivate(app serviceInitializer) func(*cobra.Command, []string) error {
return func(_ *cobra.Command, _ []string) error {
return app.Activate(cli.Context())
}
}
func Command(ctx context.Context, app serviceInitializer, storeInit func(ctx context.Context) (store.Storer, error)) *cobra.Command {
var (
enableDiscoveredProvider bool
skipValidationOnAutoDiscoveredProvider bool
clientID uint64
scope []string
tokenDuration time.Duration
)
cmd := &cobra.Command{
Use: "auth",
Short: "Authentication management",
}
autoDiscoverCmd := &cobra.Command{
Use: "auto-discovery [name] [url]",
Short: "Auto discovers new OIDC client",
Args: cobra.ExactArgs(2),
PreRunE: commandPreRunInitService(app),
Run: func(cmd *cobra.Command, args []string) {
ctx := cli.Context()
s, err := storeInit(ctx)
cli.HandleError(err)
_, err = external.RegisterOidcProvider(
ctx,
logger.Default(),
s,
app.Options().Auth,
args[0],
args[1],
true,
!skipValidationOnAutoDiscoveredProvider,
enableDiscoveredProvider,
)
cli.HandleError(err)
if enableDiscoveredProvider {
cmd.Println("OIDC provider successfully added and enabled.")
} else {
cmd.Println("OIDC provider successfully added (still disabled).")
}
},
}
autoDiscoverCmd.Flags().BoolVar(
&enableDiscoveredProvider,
"enable",
false,
"Enable this provider and external auth")
autoDiscoverCmd.Flags().BoolVar(
&skipValidationOnAutoDiscoveredProvider,
"skip-validation",
false,
"Skip validation")
jwtCmd := &cobra.Command{
Use: "jwt [email-or-id]",
Short: "Generates new JWT for a user",
Args: cobra.MinimumNArgs(1),
PreRunE: commandPreRunInitService(app),
Run: func(cmd *cobra.Command, args []string) {
ctx = auth.SetIdentityToContext(ctx, auth.ServiceUser())
var (
signedToken []byte
user *types.User
err error
userStr = args[0]
)
user, err = service.DefaultUser.FindByAny(ctx, userStr)
cli.HandleError(err)
err = service.DefaultAuth.LoadRoleMemberships(ctx, user)
cli.HandleError(err)
opts := []auth.IssueOptFn{
auth.WithIdentity(user),
auth.WithScope(scope...),
}
if clientID > 0 {
opts = append(opts, auth.WithClientID(clientID))
}
if tokenDuration > 0 {
opts = append(opts, auth.WithExpiration(tokenDuration))
}
signedToken, err = auth.TokenIssuer.Issue(ctx, opts...)
cli.HandleError(err)
cmd.Println(string(signedToken))
},
}
jwtCmd.Flags().Uint64Var(
&clientID,
"auth-client",
0,
"ID if the auth client")
jwtCmd.Flags().StringArrayVar(
&scope,
"scope",
[]string{"profile", "api"},
"Scope")
jwtCmd.Flags().DurationVar(
&tokenDuration,
"duration",
app.Options().Auth.AccessTokenLifetime,
"Token expiry duration")
testEmails := &cobra.Command{
Use: "test-notifications [recipient]",
Short: "Sends samples of all authentication notification to recipient",
Args: cobra.ExactArgs(1),
PreRunE: commandPreRunInitActivate(app),
Run: func(cmd *cobra.Command, args []string) {
var (
err error
ntf = service.DefaultAuthNotification
)
// Update current settings to be sure that we do not have outdated values
cli.HandleError(service.DefaultSettings.UpdateCurrent(ctx))
err = ntf.PasswordReset(ctx, args[0], "notification-testing-token")
cli.HandleError(err)
},
}
cmd.AddCommand(
autoDiscoverCmd,
testEmails,
jwtCmd,
assets(app),
)
return cmd
}