Refactoring and minor changes

This commit is contained in:
David Allen 2024-02-21 23:26:31 -07:00
parent 51ec06f205
commit 3735421cf9
3 changed files with 133 additions and 26 deletions

View file

@ -1,9 +1,10 @@
package cmd
import (
"davidallendj/oidc-auth/internal/api"
"davidallendj/oidc-auth/internal/oidc"
"davidallendj/oidc-auth/internal/server"
"davidallendj/oidc-auth/internal/util"
"encoding/json"
"errors"
"fmt"
"net/http"
@ -12,6 +13,10 @@ import (
"github.com/spf13/cobra"
)
var (
identitiesUrl = ""
)
var loginCmd = &cobra.Command{
Use: "login",
Short: "Start the login flow",
@ -38,7 +43,7 @@ var loginCmd = &cobra.Command{
// authorize oauth client and listen for callback from provider
fmt.Printf("Waiting for response from OIDC provider...\n")
code, err := server.WaitForAuthorizationCode(config.Host, config.Port)
code, err := api.WaitForAuthorizationCode(config.Host, config.Port)
if errors.Is(err, http.ErrServerClosed) {
fmt.Printf("server closed\n")
} else if err != nil {
@ -47,12 +52,21 @@ var loginCmd = &cobra.Command{
}
// use code from response and exchange for bearer token
server.FetchToken(code, oidcProvider.GetTokenUrl(), config.ClientId, config.ClientSecret, config.State, config.RedirectUri)
tokenString, err := api.FetchToken(code, oidcProvider.GetTokenUrl(), config.ClientId, config.ClientSecret, config.State, config.RedirectUri)
if err != nil {
fmt.Printf("%v\n", err)
return
}
// extract ID token and save user info
// create a new identity with Ory Kratos
var data map[string]any
json.Unmarshal([]byte(tokenString), &data)
idToken := data["id_token"].(string)
// create a new identity with Ory Kratos if identitiesUrl is provided
if config.IdentitiesUrl != "" {
api.CreateIdentity(config.IdentitiesUrl, idToken)
api.FetchIdentities(config.IdentitiesUrl)
}
// use ID token/user info to get access token from Ory Hydra
},
}