Added ability to get authorization code

This commit is contained in:
David Allen 2024-02-21 20:26:25 -07:00
parent bfcbca9cf1
commit 5bae300daa
9 changed files with 174 additions and 48 deletions

View file

@ -1,6 +1,75 @@
package cmd
import "github.com/spf13/cobra"
import (
"davidallendj/oidc-auth/internal/util"
"log"
"os"
"path/filepath"
"github.com/spf13/cobra"
yaml "gopkg.in/yaml.v2"
)
type Config struct {
Host string `yaml:"host"`
Port int `yaml:"port"`
RedirectUri []string `yaml:"redirect-uri"`
State string `yaml:"state"`
ResponseType string `yaml:"response-type"`
Scope []string `yaml:"scope"`
ClientId string `yaml:"client.id"`
ClientSecret string `yaml:"client.secret"`
OIDCHost string `yaml:"oidc.host"`
OIDCPort int `yaml:"oidc.port"`
}
func NewConfig() Config {
return Config{
Host: "127.0.0.1",
Port: 3333,
RedirectUri: []string{""},
State: util.RandomString(20),
ResponseType: "code",
Scope: []string{"openid", "profile", "email"},
ClientId: "",
ClientSecret: "",
OIDCHost: "127.0.0.1",
OIDCPort: 80,
}
}
func LoadConfig(path string) Config {
var c Config = NewConfig()
file, err := os.ReadFile(path)
if err != nil {
log.Printf("failed to read config file: %v\n", err)
return c
}
err = yaml.Unmarshal(file, &c)
if err != nil {
log.Fatalf("failed to unmarshal config: %v\n", err)
return c
}
return c
}
func SaveDefaultConfig(path string) {
path = filepath.Clean(path)
if path == "" || path == "." {
path = "config.yaml"
}
var c = NewConfig()
data, err := yaml.Marshal(c)
if err != nil {
log.Printf("failed to marshal config: %v\n", err)
return
}
err = os.WriteFile(path, data, os.ModePerm)
if err != nil {
log.Printf("failed to write default config file: %v\n", err)
return
}
}
var configCmd = &cobra.Command{
Use: "config",
@ -8,7 +77,7 @@ var configCmd = &cobra.Command{
Run: func(cmd *cobra.Command, args []string) {
// create a new config at all args (paths)
for _, path := range args {
_ = path
SaveDefaultConfig(path)
}
},
}

View file

@ -1,7 +1,6 @@
package cmd
import (
"davidallendj/oidc-auth/internal/oauth"
"davidallendj/oidc-auth/internal/oidc"
"davidallendj/oidc-auth/internal/server"
"davidallendj/oidc-auth/internal/util"
@ -13,36 +12,33 @@ 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) {
if configPath != "" {
config = LoadConfig(configPath)
} else {
config = NewConfig()
}
oidcProvider := oidc.NewOIDCProvider()
oidcProvider.Host = config.OIDCHost
oidcProvider.Port = config.OIDCPort
var authorizationUrl = util.BuildAuthorizationUrl(
oidcProvider.GetAuthorizeUrl(),
client.Id,
redirectUri,
util.RandomString(20),
responseType,
[]string{"email", "profile", "openid"},
config.ClientId,
config.RedirectUri,
config.State,
config.ResponseType,
config.Scope,
)
// 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
// authorize oauth client and listen for callback from provider
fmt.Printf("Waiting for response from OIDC provider...\n")
err := server.Start(host, port)
code, err := server.WaitForAuthorizationCode(config.Host, config.Port)
if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("server closed\n")
} else if err != nil {
@ -50,7 +46,8 @@ var loginCmd = &cobra.Command{
os.Exit(1)
}
// extract code from response and exchange for bearer token
// use code from response and exchange for bearer token
server.FetchToken(code, oidcProvider.GetTokenUrl(), config.ClientId, config.ClientSecret, config.State, config.RedirectUri)
// extract ID token and save user info
@ -61,12 +58,12 @@ var loginCmd = &cobra.Command{
}
func init() {
loginCmd.Flags().StringVar(&client.Id, "client.id", "", "set the client ID")
loginCmd.Flags().StringSliceVar(&redirectUri, "redirect-uri", []string{""}, "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().StringVar(&state, "state", util.RandomString(20), "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")
loginCmd.Flags().StringVar(&config.ClientId, "client.id", config.ClientId, "set the client ID")
loginCmd.Flags().StringSliceVar(&config.RedirectUri, "redirect-uri", config.RedirectUri, "set the redirect URI")
loginCmd.Flags().StringVar(&config.ResponseType, "response-type", config.ResponseType, "set the response-type")
loginCmd.Flags().StringSliceVar(&config.Scope, "scope", config.Scope, "set the scopes")
loginCmd.Flags().StringVar(&config.State, "state", config.State, "set the state")
loginCmd.Flags().StringVar(&config.Host, "host", config.Host, "set the listening host")
loginCmd.Flags().IntVar(&config.Port, "port", config.Port, "set the listening port")
rootCmd.AddCommand(loginCmd)
}

View file

@ -7,7 +7,10 @@ import (
"github.com/spf13/cobra"
)
var configPath = ""
var (
configPath = ""
config Config
)
var rootCmd = &cobra.Command{
Use: "oidc",
Short: "An experimental OIDC helper tool for handling logins",
@ -18,11 +21,11 @@ var rootCmd = &cobra.Command{
func Execute() {
if err := rootCmd.Execute(); err != nil {
fmt.Fprintf(os.Stderr, "Whoops. There was an error while executing your CLI '%s'", err)
fmt.Fprintf(os.Stderr, "failed to start CLI: %s", err)
os.Exit(1)
}
}
func init() {
rootCmd.Flags().StringVar(&configPath, "config", "", "set the config path")
rootCmd.PersistentFlags().StringVar(&configPath, "config", "", "set the config path")
}