| OLD | NEW |
| 1 // Copyright 2015 The LUCI Authors. All rights reserved. | 1 // Copyright 2015 The LUCI Authors. All rights reserved. |
| 2 // Use of this source code is governed under the Apache License, Version 2.0 | 2 // Use of this source code is governed under the Apache License, Version 2.0 |
| 3 // that can be found in the LICENSE file. | 3 // that can be found in the LICENSE file. |
| 4 | 4 |
| 5 // Package authcli implements authentication related CLI subcommands. | 5 // Package authcli implements authentication related CLI subcommands. |
| 6 // | 6 // |
| 7 // It can be used from CLI tools that want customize authentication | 7 // It can be used from CLI tools that want customize authentication |
| 8 // configuration from the command line. | 8 // configuration from the command line. |
| 9 // | 9 // |
| 10 // It use luci-go/common/cli.GetContext() to grab a context for logging, so | 10 // It use luci-go/common/cli.GetContext() to grab a context for logging, so |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 "github.com/maruel/subcommands" | 53 "github.com/maruel/subcommands" |
| 54 "golang.org/x/net/context" | 54 "golang.org/x/net/context" |
| 55 "golang.org/x/net/context/ctxhttp" | 55 "golang.org/x/net/context/ctxhttp" |
| 56 | 56 |
| 57 "github.com/luci/luci-go/common/auth" | 57 "github.com/luci/luci-go/common/auth" |
| 58 "github.com/luci/luci-go/common/cli" | 58 "github.com/luci/luci-go/common/cli" |
| 59 ) | 59 ) |
| 60 | 60 |
| 61 // CommandParams specifies various parameters for a subcommand. | 61 // CommandParams specifies various parameters for a subcommand. |
| 62 type CommandParams struct { | 62 type CommandParams struct { |
| 63 » Name string // name of the subcommand. | 63 » Name string // name of the subcommand. |
| 64 » Advanced bool // subcommands should treat this as an 'advanced' comman
d |
| 64 | 65 |
| 65 AuthOptions auth.Options // default auth options. | 66 AuthOptions auth.Options // default auth options. |
| 66 | 67 |
| 67 // ScopesFlag specifies if -scope flag must be registered. | 68 // ScopesFlag specifies if -scope flag must be registered. |
| 68 // AuthOptions.Scopes is used as a default value. | 69 // AuthOptions.Scopes is used as a default value. |
| 69 // If it is empty, defaults to "https://www.googleapis.com/auth/userinfo
.email". | 70 // If it is empty, defaults to "https://www.googleapis.com/auth/userinfo
.email". |
| 70 ScopesFlag bool | 71 ScopesFlag bool |
| 71 } | 72 } |
| 72 | 73 |
| 73 // Flags defines command line flags related to authentication. | 74 // Flags defines command line flags related to authentication. |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 params *CommandParams | 114 params *CommandParams |
| 114 } | 115 } |
| 115 | 116 |
| 116 func (c *commandRunBase) registerBaseFlags() { | 117 func (c *commandRunBase) registerBaseFlags() { |
| 117 c.flags.registerScopesFlag = c.params.ScopesFlag | 118 c.flags.registerScopesFlag = c.params.ScopesFlag |
| 118 c.flags.Register(&c.Flags, c.params.AuthOptions) | 119 c.flags.Register(&c.Flags, c.params.AuthOptions) |
| 119 } | 120 } |
| 120 | 121 |
| 121 // SubcommandLogin returns subcommands.Command that can be used to perform | 122 // SubcommandLogin returns subcommands.Command that can be used to perform |
| 122 // interactive login. | 123 // interactive login. |
| 123 func SubcommandLogin(opts auth.Options, name string) *subcommands.Command { | 124 func SubcommandLogin(opts auth.Options, name string, advanced bool) *subcommands
.Command { |
| 124 » return SubcommandLoginWithParams(CommandParams{Name: name, AuthOptions:
opts}) | 125 » return SubcommandLoginWithParams(CommandParams{Name: name, Advanced: adv
anced, AuthOptions: opts}) |
| 125 } | 126 } |
| 126 | 127 |
| 127 // SubcommandLoginWithParams returns subcommands.Command that can be used to per
form | 128 // SubcommandLoginWithParams returns subcommands.Command that can be used to per
form |
| 128 // interactive login. | 129 // interactive login. |
| 129 func SubcommandLoginWithParams(params CommandParams) *subcommands.Command { | 130 func SubcommandLoginWithParams(params CommandParams) *subcommands.Command { |
| 130 return &subcommands.Command{ | 131 return &subcommands.Command{ |
| 132 Advanced: params.Advanced, |
| 131 UsageLine: params.Name, | 133 UsageLine: params.Name, |
| 132 ShortDesc: "performs interactive login flow", | 134 ShortDesc: "performs interactive login flow", |
| 133 LongDesc: "Performs interactive login flow and caches obtained
credentials", | 135 LongDesc: "Performs interactive login flow and caches obtained
credentials", |
| 134 CommandRun: func() subcommands.CommandRun { | 136 CommandRun: func() subcommands.CommandRun { |
| 135 c := &loginRun{} | 137 c := &loginRun{} |
| 136 c.params = ¶ms | 138 c.params = ¶ms |
| 137 c.registerBaseFlags() | 139 c.registerBaseFlags() |
| 138 return c | 140 return c |
| 139 }, | 141 }, |
| 140 } | 142 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 160 if canReportIdentity(opts.Scopes) { | 162 if canReportIdentity(opts.Scopes) { |
| 161 if err = reportIdentity(ctx, authenticator); err != nil { | 163 if err = reportIdentity(ctx, authenticator); err != nil { |
| 162 return 3 | 164 return 3 |
| 163 } | 165 } |
| 164 } | 166 } |
| 165 return 0 | 167 return 0 |
| 166 } | 168 } |
| 167 | 169 |
| 168 // SubcommandLogout returns subcommands.Command that can be used to purge cached | 170 // SubcommandLogout returns subcommands.Command that can be used to purge cached |
| 169 // credentials. | 171 // credentials. |
| 170 func SubcommandLogout(opts auth.Options, name string) *subcommands.Command { | 172 func SubcommandLogout(opts auth.Options, name string, advanced bool) *subcommand
s.Command { |
| 171 » return SubcommandLogoutWithParams(CommandParams{Name: name, AuthOptions:
opts}) | 173 » return SubcommandLogoutWithParams(CommandParams{Name: name, Advanced: ad
vanced, AuthOptions: opts}) |
| 172 } | 174 } |
| 173 | 175 |
| 174 // SubcommandLogoutWithParams returns subcommands.Command that can be used to pu
rge cached | 176 // SubcommandLogoutWithParams returns subcommands.Command that can be used to pu
rge cached |
| 175 // credentials. | 177 // credentials. |
| 176 func SubcommandLogoutWithParams(params CommandParams) *subcommands.Command { | 178 func SubcommandLogoutWithParams(params CommandParams) *subcommands.Command { |
| 177 return &subcommands.Command{ | 179 return &subcommands.Command{ |
| 180 Advanced: params.Advanced, |
| 178 UsageLine: params.Name, | 181 UsageLine: params.Name, |
| 179 ShortDesc: "removes cached credentials", | 182 ShortDesc: "removes cached credentials", |
| 180 LongDesc: "Removes cached credentials from the disk", | 183 LongDesc: "Removes cached credentials from the disk", |
| 181 CommandRun: func() subcommands.CommandRun { | 184 CommandRun: func() subcommands.CommandRun { |
| 182 c := &logoutRun{} | 185 c := &logoutRun{} |
| 183 c.params = ¶ms | 186 c.params = ¶ms |
| 184 c.registerBaseFlags() | 187 c.registerBaseFlags() |
| 185 return c | 188 return c |
| 186 }, | 189 }, |
| 187 } | 190 } |
| (...skipping 13 matching lines...) Expand all Loading... |
| 201 err = auth.NewAuthenticator(ctx, auth.SilentLogin, opts).PurgeCredential
sCache() | 204 err = auth.NewAuthenticator(ctx, auth.SilentLogin, opts).PurgeCredential
sCache() |
| 202 if err != nil { | 205 if err != nil { |
| 203 fmt.Fprintln(os.Stderr, err) | 206 fmt.Fprintln(os.Stderr, err) |
| 204 return 2 | 207 return 2 |
| 205 } | 208 } |
| 206 return 0 | 209 return 0 |
| 207 } | 210 } |
| 208 | 211 |
| 209 // SubcommandInfo returns subcommand.Command that can be used to print current | 212 // SubcommandInfo returns subcommand.Command that can be used to print current |
| 210 // cached credentials. | 213 // cached credentials. |
| 211 func SubcommandInfo(opts auth.Options, name string) *subcommands.Command { | 214 func SubcommandInfo(opts auth.Options, name string, advanced bool) *subcommands.
Command { |
| 212 » return SubcommandInfoWithParams(CommandParams{Name: name, AuthOptions: o
pts}) | 215 » return SubcommandInfoWithParams(CommandParams{Name: name, Advanced: adva
nced, AuthOptions: opts}) |
| 213 } | 216 } |
| 214 | 217 |
| 215 // SubcommandInfoWithParams returns subcommand.Command that can be used to print
current | 218 // SubcommandInfoWithParams returns subcommand.Command that can be used to print
current |
| 216 // cached credentials. | 219 // cached credentials. |
| 217 func SubcommandInfoWithParams(params CommandParams) *subcommands.Command { | 220 func SubcommandInfoWithParams(params CommandParams) *subcommands.Command { |
| 218 return &subcommands.Command{ | 221 return &subcommands.Command{ |
| 222 Advanced: params.Advanced, |
| 219 UsageLine: params.Name, | 223 UsageLine: params.Name, |
| 220 ShortDesc: "prints an email address associated with currently ca
ched token", | 224 ShortDesc: "prints an email address associated with currently ca
ched token", |
| 221 LongDesc: "Prints an email address associated with currently ca
ched token", | 225 LongDesc: "Prints an email address associated with currently ca
ched token", |
| 222 CommandRun: func() subcommands.CommandRun { | 226 CommandRun: func() subcommands.CommandRun { |
| 223 c := &infoRun{} | 227 c := &infoRun{} |
| 224 c.params = ¶ms | 228 c.params = ¶ms |
| 225 c.registerBaseFlags() | 229 c.registerBaseFlags() |
| 226 return c | 230 return c |
| 227 }, | 231 }, |
| 228 } | 232 } |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 // SubcommandToken returns subcommand.Command that can be used to print current | 267 // SubcommandToken returns subcommand.Command that can be used to print current |
| 264 // access token. | 268 // access token. |
| 265 func SubcommandToken(opts auth.Options, name string) *subcommands.Command { | 269 func SubcommandToken(opts auth.Options, name string) *subcommands.Command { |
| 266 return SubcommandTokenWithParams(CommandParams{Name: name, AuthOptions:
opts}) | 270 return SubcommandTokenWithParams(CommandParams{Name: name, AuthOptions:
opts}) |
| 267 } | 271 } |
| 268 | 272 |
| 269 // SubcommandTokenWithParams returns subcommand.Command that can be used to prin
t current | 273 // SubcommandTokenWithParams returns subcommand.Command that can be used to prin
t current |
| 270 // access token. | 274 // access token. |
| 271 func SubcommandTokenWithParams(params CommandParams) *subcommands.Command { | 275 func SubcommandTokenWithParams(params CommandParams) *subcommands.Command { |
| 272 return &subcommands.Command{ | 276 return &subcommands.Command{ |
| 277 Advanced: params.Advanced, |
| 273 UsageLine: params.Name, | 278 UsageLine: params.Name, |
| 274 ShortDesc: "prints an access token", | 279 ShortDesc: "prints an access token", |
| 275 LongDesc: "Generates an access token if requested and prints it
.", | 280 LongDesc: "Generates an access token if requested and prints it
.", |
| 276 CommandRun: func() subcommands.CommandRun { | 281 CommandRun: func() subcommands.CommandRun { |
| 277 c := &tokenRun{} | 282 c := &tokenRun{} |
| 278 c.params = ¶ms | 283 c.params = ¶ms |
| 279 c.registerBaseFlags() | 284 c.registerBaseFlags() |
| 280 c.Flags.DurationVar( | 285 c.Flags.DurationVar( |
| 281 &c.lifetime, "lifetime", time.Minute, | 286 &c.lifetime, "lifetime", time.Minute, |
| 282 "Minimum token lifetime. If existing token expir
ed and refresh token or service account is not present, returns nothing.", | 287 "Minimum token lifetime. If existing token expir
ed and refresh token or service account is not present, returns nothing.", |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 420 fmt.Printf("Logged in as %s.\n", reply.Email) | 425 fmt.Printf("Logged in as %s.\n", reply.Email) |
| 421 fmt.Printf("OAuth token details:\n") | 426 fmt.Printf("OAuth token details:\n") |
| 422 fmt.Printf(" Client ID: %s\n", reply.IssuedTo) | 427 fmt.Printf(" Client ID: %s\n", reply.IssuedTo) |
| 423 fmt.Printf(" Scopes:\n") | 428 fmt.Printf(" Scopes:\n") |
| 424 for _, scope := range strings.Split(reply.Scope, " ") { | 429 for _, scope := range strings.Split(reply.Scope, " ") { |
| 425 fmt.Printf(" %s\n", scope) | 430 fmt.Printf(" %s\n", scope) |
| 426 } | 431 } |
| 427 | 432 |
| 428 return nil | 433 return nil |
| 429 } | 434 } |
| OLD | NEW |