Added CLI and more functionality

This commit is contained in:
David Allen 2024-02-21 17:51:59 -07:00
parent c04107cf3d
commit 053095c412
12 changed files with 199 additions and 92 deletions

18
cmd/config.go Normal file
View file

@ -0,0 +1,18 @@
package cmd
import "github.com/spf13/cobra"
var configCmd = &cobra.Command{
Use: "config",
Short: "Create a new default config file",
Run: func(cmd *cobra.Command, args []string) {
// create a new config at all args (paths)
for _, path := range args {
_ = path
}
},
}
func init() {
rootCmd.AddCommand(configCmd)
}

62
cmd/login Normal file
View file

@ -0,0 +1,62 @@
package cmd
import "github.com/spf13/cobra"
var (
host string
port int
redirectUri = []string{""}
state = ""
responseType = "code"
scope = []string{"email", "profile", "openid"}
client oauth.Client
)
var loginCmd = &cobra.Command{
Use: "login",
Short: "Start the login flow"
Run: func(cmd *cobra.Command, args []string) {
oidcProvider := oidc.NewOIDCProvider()
var authorizationUrl = util.BuildAuthorizationUrl(
oidcProvider.GetAuthorizeUrl(),
client.Id,
redirectUri,
util.RandomString(20),
responseType,
[]string{"email", "profile", "openid"},
)
// print the authorization URL for the user to log in
fmt.Printf("Login with identity provider: %s\n", authorizationUrl)
// start a HTTP server to listen for callback responses
fmt.Printf("Waiting for response from OIDC provider...\n")
err := server.Start(host, port)
if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("server closed\n")
} else if err != nil {
fmt.Printf("error starting server: %s\n", err)
os.Exit(1)
}
// extract code from response and exchange for bearer token
// extract ID token and save user info
// create a new identity with Ory Kratos
// use ID token/user info to get access token from Ory Hydra
},
}
func init(){
loginCmd.Flags().StringVar(&client.Id, "client.id", "", "set the client ID")
loginCmd.Flags().StringVar(&redirectUri, "redirect-uri", "", "set the redirect URI")
loginCmd.Flags().StringVar(&responseType, "response-type", "code", "set the response-type")
loginCmd.Flags().StringSliceVar(&scope, "scope", []string{"openid", "email"}, "set the scopes")
loginCmd.Flags().String(&state, "state", util.RandomString(), "set the state")
loginCmd.Flags().StringVar(host, "host", "127.0.0.1", "set the listening host")
loginCmd.Flags().IntVar(&port, "port", 3333, "set the listening port")
rootCmd.AddCommand(loginCmd)
}

28
cmd/root.go Normal file
View file

@ -0,0 +1,28 @@
package cmd
import (
"fmt"
"os"
"github.com/spf13/cobra"
)
var configPath = ""
var rootCmd = &cobra.Command{
Use: "oidc",
Short: "An experimental OIDC helper tool for handling logins",
Run: func(cmd *cobra.Command, args []string) {
},
}
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Whoops. There was an error while executing your CLI '%s'", err)
os.Exit(1)
}
}
func init() {
rootCmd.Flags().StringVar(&configPath, "config", "", "set the config path")
}