mirror of
https://github.com/davidallendj/opaal.git
synced 2025-12-20 03:27:02 -07:00
Added ability to get authorization code
This commit is contained in:
parent
bfcbca9cf1
commit
5bae300daa
9 changed files with 174 additions and 48 deletions
49
cmd/login.go
49
cmd/login.go
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue