author | Mikael Berthe <mikael@lilotux.net> |
Sun, 30 Jun 2024 17:57:48 +0200 | |
changeset 282 | 46b37f928ab9 |
parent 265 | 05c40b36d3b2 |
permissions | -rw-r--r-- |
265
05c40b36d3b2
Bump to version 3.0.0-dev, using madon v3
Mikael Berthe <mikael@lilotux.net>
parents:
245
diff
changeset
|
1 |
// Copyright © 2017-2023 Mikael Berthe <mikael@lilotux.net> |
113 | 2 |
// |
3 |
// Licensed under the MIT license. |
|
4 |
// Please see the LICENSE file is this directory. |
|
5 |
||
6 |
package cmd |
|
7 |
||
8 |
import ( |
|
9 |
"fmt" |
|
10 |
"os" |
|
11 |
||
12 |
"github.com/pkg/errors" |
|
13 |
"github.com/spf13/cobra" |
|
14 |
) |
|
15 |
||
16 |
var oauth2Cmd = &cobra.Command{ |
|
17 |
Use: "oauth2", |
|
18 |
Short: "OAuth2 authentication/authorization", |
|
127
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
19 |
Example: ` madonctl oauth2 # Interactive OAuth2 login |
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
20 |
madonctl oauth2 get-url # Display OAuth2 auhtorization URL |
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
21 |
madonctl oauth2 code CODE # Enter OAuth2 code |
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
22 |
|
2b4d0f198a94
Update online documention to config and oauth2 commands
Mikael Berthe <mikael@lilotux.net>
parents:
113
diff
changeset
|
23 |
madonctl oauth2 > config.yaml # Redirect to configuration file`, |
113 | 24 |
RunE: func(cmd *cobra.Command, args []string) error { |
25 |
return oAuth2Interactive(args) |
|
26 |
}, |
|
27 |
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { |
|
28 |
// Initialize application; do not log in yet |
|
29 |
return madonInit(false) |
|
30 |
}, |
|
31 |
} |
|
32 |
||
33 |
func init() { |
|
34 |
RootCmd.AddCommand(oauth2Cmd) |
|
35 |
||
36 |
// Subcommands |
|
37 |
oauth2Cmd.AddCommand(oauth2Subcommands...) |
|
38 |
} |
|
39 |
||
40 |
var oauth2Subcommands = []*cobra.Command{ |
|
41 |
&cobra.Command{ |
|
42 |
Use: "get-url", |
|
43 |
Short: "Get OAuth2 URL", |
|
44 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
45 |
return oAuth2GetURL() |
|
46 |
}, |
|
47 |
}, |
|
48 |
&cobra.Command{ |
|
243
1bec7d3d6a85
Improve oauth2 code usage message
Mikael Berthe <mikael@lilotux.net>
parents:
185
diff
changeset
|
49 |
Use: "code CODE", |
113 | 50 |
Short: "Log in with OAuth2 code", |
51 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
52 |
return oAuth2ExchangeCode(args) |
|
53 |
}, |
|
54 |
}, |
|
55 |
} |
|
56 |
||
57 |
func oAuth2GetURL() error { |
|
58 |
// (gClient != nil thanks to PreRun) |
|
59 |
||
60 |
url, err := gClient.LoginOAuth2("", scopes) |
|
61 |
if err != nil { |
|
62 |
return errors.Wrap(err, "OAuth2 authentication failed") |
|
63 |
} |
|
64 |
||
65 |
fmt.Printf("%s\n", url) |
|
66 |
return nil |
|
67 |
} |
|
68 |
||
69 |
func oAuth2ExchangeCode(args []string) error { |
|
70 |
// (gClient != nil thanks to PreRun) |
|
71 |
||
72 |
if len(args) != 1 { |
|
73 |
return errors.New("wrong usage: code needs 1 argument") |
|
74 |
} |
|
75 |
||
76 |
code := args[0] |
|
77 |
||
78 |
if code == "" { |
|
79 |
return errors.New("no code entered") |
|
80 |
} |
|
81 |
||
82 |
// The code has been set; proceed with token exchange |
|
83 |
_, err := gClient.LoginOAuth2(code, scopes) |
|
84 |
if err != nil { |
|
85 |
return err |
|
86 |
} |
|
87 |
||
88 |
if gClient.UserToken != nil { |
|
89 |
errPrint("Login successful.\n") |
|
245
910f00ab2799
Fix oauth2 not displaying the new token
Mikael Berthe <mikael@lilotux.net>
parents:
244
diff
changeset
|
90 |
errPrint("The new token is %s.\n", gClient.UserToken.AccessToken) |
244
a01bc98ae01a
Display config details after oauth2
Mikael Berthe <mikael@lilotux.net>
parents:
243
diff
changeset
|
91 |
configDump(true) |
113 | 92 |
} |
93 |
return nil |
|
94 |
} |
|
95 |
||
96 |
// oAuth2Interactive is the default behaviour |
|
97 |
func oAuth2Interactive(args []string) error { |
|
98 |
// (gClient != nil thanks to PreRun) |
|
99 |
||
100 |
url, err := gClient.LoginOAuth2("", scopes) |
|
101 |
if err != nil { |
|
102 |
return errors.Wrap(err, "OAuth2 authentication failed") |
|
103 |
} |
|
104 |
||
105 |
fmt.Fprintf(os.Stderr, "Visit the URL for the auth dialog:\n%s\n", url) |
|
106 |
fmt.Fprintf(os.Stderr, "Enter code: ") |
|
107 |
var code string |
|
108 |
if _, err := fmt.Scan(&code); err != nil { |
|
109 |
return err |
|
110 |
} |
|
111 |
||
112 |
if code == "" { |
|
113 |
return errors.New("no code entered") |
|
114 |
} |
|
115 |
||
116 |
// The code has been set; proceed with token exchange |
|
117 |
return oAuth2ExchangeCode([]string{code}) |
|
118 |
} |