Begin auth support.
authorOllivier Robert <roberto@keltia.net>
Wed, 12 Apr 2017 18:07:56 +0200
changeset 82 09f5e04b1b37
parent 81 ba90955d2d56
child 89 8a7a33bec6e1
Begin auth support.
cmd/gondole-cli/main.go
gondole.go
--- a/cmd/gondole-cli/main.go	Wed Apr 12 14:27:09 2017 +0200
+++ b/cmd/gondole-cli/main.go	Wed Apr 12 18:07:56 2017 +0200
@@ -9,9 +9,10 @@
 )
 
 var (
-	fVerbose  bool
-	fInstance string
-	fScopes   string
+	fVerbose    bool
+	fAuthMethod string
+	fInstance   string
+	fScopes     string
 
 	instance *gondole.Client
 	cnf      *Server
@@ -29,6 +30,11 @@
 		"write",
 		"follow",
 	}
+
+	authMethods = map[string]bool{
+		"basic":  true,
+		"oauth2": true,
+	}
 )
 
 // Config holds our parameters
@@ -41,9 +47,17 @@
 
 type Config struct {
 	Default string
+
+	// Can be "oauth2", "basic"
+	Auth string
+
+	// If not using OAuth2
+	User     string
+	Password string
 }
 
 func setupEnvironment(c *cli.Context) (err error) {
+	var config Config
 	var scopes []string
 
 	if fInstance != "" {
@@ -51,14 +65,22 @@
 		APIEndpoint = filterURL(fInstance)
 	}
 
+	if fAuthMethod != "" && authMethods[fAuthMethod] {
+
+	}
+
 	// Load configuration, will register if none is found
 	cnf, err = LoadConfig(InstanceName)
 	if err != nil {
 		// Nothing exist yet
-		defName := Config{
-			Default: InstanceName,
+		config := Config{
+			Default:  InstanceName,
+			Auth:     "basic",
+			User:     "",
+			Password: "",
 		}
-		err = defName.Write()
+
+		err = config.Write()
 		if err != nil {
 			log.Fatalf("error: can not write config for %s", InstanceName)
 		}
@@ -94,6 +116,8 @@
 
 	}
 	// Log in to the instance
+	err = instance.Login()
+
 	return err
 }
 
@@ -117,10 +141,10 @@
 	app.Before = setupEnvironment
 
 	app.Flags = []cli.Flag{
-		cli.BoolFlag{
-			Name:        "verbose,v",
-			Usage:       "verbose mode",
-			Destination: &fVerbose,
+		cli.StringFlag{
+			Name:        "auth,A",
+			Usage:       "authentication mode",
+			Destination: &fAuthMethod,
 		},
 		cli.StringFlag{
 			Name:        "instance,I",
@@ -132,6 +156,11 @@
 			Usage:       "use these scopes",
 			Destination: &fScopes,
 		},
+		cli.BoolFlag{
+			Name:        "verbose,v",
+			Usage:       "verbose mode",
+			Destination: &fVerbose,
+		},
 	}
 	app.Run(os.Args)
 }
--- a/gondole.go	Wed Apr 12 14:27:09 2017 +0200
+++ b/gondole.go	Wed Apr 12 18:07:56 2017 +0200
@@ -51,3 +51,7 @@
 	}
 	return
 }
+
+func (g *Client) Login() (err error) {
+	return
+}