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