113
|
1 |
// Copyright © 2017 Mikael Berthe <mikael@lilotux.net> |
|
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 |
//"github.com/McKael/madonctl/printer" |
|
15 |
) |
|
16 |
|
|
17 |
var oauth2Cmd = &cobra.Command{ |
|
18 |
Use: "oauth2", |
|
19 |
Short: "OAuth2 authentication/authorization", |
|
20 |
Example: ` madonctl oauth2 # Interactive OAuth2 login |
|
21 |
madonctl oauth2 get-url # Display OAuth2 auhtorization URL |
|
22 |
madonctl oauth2 code CODE # Enter OAuth2 code`, |
|
23 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
24 |
return oAuth2Interactive(args) |
|
25 |
}, |
|
26 |
PersistentPreRunE: func(cmd *cobra.Command, args []string) error { |
|
27 |
// Initialize application; do not log in yet |
|
28 |
return madonInit(false) |
|
29 |
}, |
|
30 |
} |
|
31 |
|
|
32 |
func init() { |
|
33 |
RootCmd.AddCommand(oauth2Cmd) |
|
34 |
|
|
35 |
// Subcommands |
|
36 |
oauth2Cmd.AddCommand(oauth2Subcommands...) |
|
37 |
} |
|
38 |
|
|
39 |
var oauth2Subcommands = []*cobra.Command{ |
|
40 |
&cobra.Command{ |
|
41 |
Use: "get-url", |
|
42 |
Short: "Get OAuth2 URL", |
|
43 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
44 |
return oAuth2GetURL() |
|
45 |
}, |
|
46 |
}, |
|
47 |
&cobra.Command{ |
|
48 |
Use: "code", |
|
49 |
Short: "Log in with OAuth2 code", |
|
50 |
RunE: func(cmd *cobra.Command, args []string) error { |
|
51 |
return oAuth2ExchangeCode(args) |
|
52 |
}, |
|
53 |
}, |
|
54 |
} |
|
55 |
|
|
56 |
func oAuth2GetURL() error { |
|
57 |
// (gClient != nil thanks to PreRun) |
|
58 |
|
|
59 |
url, err := gClient.LoginOAuth2("", scopes) |
|
60 |
if err != nil { |
|
61 |
return errors.Wrap(err, "OAuth2 authentication failed") |
|
62 |
} |
|
63 |
|
|
64 |
fmt.Printf("%s\n", url) |
|
65 |
return nil |
|
66 |
} |
|
67 |
|
|
68 |
func oAuth2ExchangeCode(args []string) error { |
|
69 |
// (gClient != nil thanks to PreRun) |
|
70 |
|
|
71 |
if len(args) != 1 { |
|
72 |
return errors.New("wrong usage: code needs 1 argument") |
|
73 |
} |
|
74 |
|
|
75 |
code := args[0] |
|
76 |
|
|
77 |
if code == "" { |
|
78 |
return errors.New("no code entered") |
|
79 |
} |
|
80 |
|
|
81 |
// The code has been set; proceed with token exchange |
|
82 |
_, err := gClient.LoginOAuth2(code, scopes) |
|
83 |
if err != nil { |
|
84 |
return err |
|
85 |
} |
|
86 |
|
|
87 |
if gClient.UserToken != nil { |
|
88 |
errPrint("Login successful.\n") |
|
89 |
configDump() |
|
90 |
} |
|
91 |
return nil |
|
92 |
} |
|
93 |
|
|
94 |
// oAuth2Interactive is the default behaviour |
|
95 |
func oAuth2Interactive(args []string) error { |
|
96 |
// (gClient != nil thanks to PreRun) |
|
97 |
|
|
98 |
url, err := gClient.LoginOAuth2("", scopes) |
|
99 |
if err != nil { |
|
100 |
return errors.Wrap(err, "OAuth2 authentication failed") |
|
101 |
} |
|
102 |
|
|
103 |
fmt.Fprintf(os.Stderr, "Visit the URL for the auth dialog:\n%s\n", url) |
|
104 |
fmt.Fprintf(os.Stderr, "Enter code: ") |
|
105 |
var code string |
|
106 |
if _, err := fmt.Scan(&code); err != nil { |
|
107 |
return err |
|
108 |
} |
|
109 |
|
|
110 |
if code == "" { |
|
111 |
return errors.New("no code entered") |
|
112 |
} |
|
113 |
|
|
114 |
// The code has been set; proceed with token exchange |
|
115 |
return oAuth2ExchangeCode([]string{code}) |
|
116 |
} |