This commit is contained in:
2024-06-23 16:27:55 -05:00
parent d96fae1276
commit 125e0dc9cf
9 changed files with 167 additions and 83 deletions

View File

@@ -1,6 +0,0 @@
auth:
clientid: "6y8JS7qJ5MmpcT2uEje2A7eQ7AB25Q1GEs3CtXPf"
granttype: client_credentials
password: ""
url: https://authentik.durp.info/application/o/token/
username: ""

View File

@@ -4,6 +4,22 @@ import (
"github.com/spf13/cobra"
)
type accessTokenResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
IDToken string `json:"id_token"`
}
var (
clientID string
grantType string
url string
username string
password string
scope string
)
var AuthCmd = &cobra.Command{
Use: "auth",
Short: "All things Authorization",

View File

@@ -1,74 +0,0 @@
package auth
import (
"context"
"fmt"
"net/http"
"github.com/cli/oauth/device"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
type accessTokenResponse struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
ExpiresIn int `json:"expires_in"`
IDToken string `json:"id_token"`
}
var (
clientID string
grantType string
url string
username string
password string
scope string
)
var generateTokenCmd = &cobra.Command{
Use: "generateToken",
Short: "Prints disk usage of the current directory",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if clientID == "" {
clientID = viper.GetViper().GetString("auth.clientID")
}
generateToken(clientID)
},
}
func init() {
generateTokenCmd.Flags().StringVarP(&clientID, "clientID", "c", "", "The ClientID")
generateTokenCmd.Flags().
StringVarP(&grantType, "grantType", "g", "client_credentials", "The Grant Type")
generateTokenCmd.Flags().StringVarP(&url, "url", "", "", "Token URL")
AuthCmd.AddCommand(generateTokenCmd)
}
func generateToken(
clientID string,
) {
scopes := []string{"openid", "email", "profile"}
httpClient := http.DefaultClient
code, err := device.RequestCode(httpClient, "https://authentik.durp.info/application/o/device/", clientID, scopes)
if err != nil {
panic(err)
}
fmt.Printf("Copy code: %s\n", code.UserCode)
fmt.Printf("then open: %s\n", code.VerificationURIComplete)
accessToken, err := device.Wait(context.TODO(), httpClient, "https://authentik.durp.info/application/o/token/", device.WaitOptions{
ClientID: clientID,
DeviceCode: code,
GrantType: "urn:ietf:params:oauth:grant-type:device_code",
})
if err != nil {
panic(err)
}
fmt.Printf("Access token: %s\n", accessToken.Token)
}

37
cmd/auth/gettoken.go Normal file
View File

@@ -0,0 +1,37 @@
package auth
import (
"fmt"
"log"
"os/user"
"github.com/spf13/cobra"
"github.com/zalando/go-keyring"
)
var getTokenCmd = &cobra.Command{
Use: "get token",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
gettokn()
},
}
func init() {
AuthCmd.AddCommand(getTokenCmd)
}
func gettokn() {
service := "durpcli"
user, _ := user.Current()
token, err := keyring.Get(service, user.Username)
if err != nil {
log.Fatal(err)
}
fmt.Println(token)
}

63
cmd/auth/login.go Normal file
View File

@@ -0,0 +1,63 @@
package auth
import (
"context"
"log"
"net/http"
"os/exec"
"os/user"
"github.com/cli/oauth/device"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/zalando/go-keyring"
)
var loginCmd = &cobra.Command{
Use: "login",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
if clientID == "" {
clientID = viper.GetViper().GetString("auth.clientID")
}
login(clientID)
},
}
func init() {
AuthCmd.Flags().StringVarP(&clientID, "clientID", "c", "", "The ClientID")
AuthCmd.AddCommand(loginCmd)
}
func login(clientID string) {
service := "durpcli"
user, _ := user.Current()
scopes := []string{"openid", "email", "profile"}
httpClient := http.DefaultClient
code, err := device.RequestCode(httpClient, "https://authentik.durp.info/application/o/device/", clientID, scopes)
if err != nil {
panic(err)
}
exec.Command("open", code.VerificationURIComplete).Run()
accessToken, err := device.Wait(context.TODO(), httpClient, "https://authentik.durp.info/application/o/token/", device.WaitOptions{
ClientID: clientID,
DeviceCode: code,
GrantType: "urn:ietf:params:oauth:grant-type:device_code",
})
if err != nil {
panic(err)
}
err = keyring.Set(service, user.Username, accessToken.Token)
if err != nil {
log.Fatal(err)
}
}

30
cmd/auth/logout.go Normal file
View File

@@ -0,0 +1,30 @@
package auth
import (
"os/user"
"github.com/spf13/cobra"
"github.com/zalando/go-keyring"
)
var logoutCmd = &cobra.Command{
Use: "logout",
Short: "",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
logout()
},
}
func init() {
AuthCmd.AddCommand(logoutCmd)
}
func logout() {
service := "durpcli"
user, _ := user.Current()
keyring.Delete(service, user.Username)
}

View File

@@ -1,7 +1,8 @@
package cfg
import (
"fmt"
"os"
"os/user"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@@ -12,10 +13,14 @@ var newcfgcmd = &cobra.Command{
Short: "Generates a config file using current config",
Long: ``,
Run: func(cmd *cobra.Command, args []string) {
err := viper.WriteConfigAs(".DurpCLI.yaml")
usr, err := user.Current()
if err != nil {
fmt.Println(err)
os.Exit(1)
}
viper.SetConfigType("yaml")
viper.SetConfigName(".durpcli")
viper.AddConfigPath(usr.HomeDir)
viper.WriteConfig()
},
}

4
go.mod
View File

@@ -9,17 +9,21 @@ require (
github.com/cli/oauth v1.0.1
github.com/spf13/cobra v1.8.1
github.com/spf13/viper v1.19.0
github.com/zalando/go-keyring v0.2.5
)
require (
github.com/alessio/shellescape v1.4.1 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/charmbracelet/x/ansi v0.1.2 // indirect
github.com/charmbracelet/x/input v0.1.0 // indirect
github.com/charmbracelet/x/term v0.1.1 // indirect
github.com/charmbracelet/x/windows v0.1.0 // indirect
github.com/danieljoos/wincred v1.2.0 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/godbus/dbus/v5 v5.1.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect

9
go.sum
View File

@@ -1,3 +1,5 @@
github.com/alessio/shellescape v1.4.1 h1:V7yhSDDn8LP4lc4jS8pFkt0zCnzVJlG5JXy9BVKJUX0=
github.com/alessio/shellescape v1.4.1/go.mod h1:PZAiSCk0LJaZkiCSkPv8qIobYglO3FPpyFjDCtHLS30=
github.com/atotto/clipboard v0.1.4 h1:EH0zSVneZPSuFR11BlR9YppQTVDbh5+16AmcJi4g1z4=
github.com/atotto/clipboard v0.1.4/go.mod h1:ZY9tmq7sm5xIbd9bOK4onWV4S6X0u6GY7Vn0Yu86PYI=
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
@@ -21,6 +23,8 @@ github.com/cli/oauth v1.0.1 h1:pXnTFl/qUegXHK531Dv0LNjW4mLx626eS42gnzfXJPA=
github.com/cli/oauth v1.0.1/go.mod h1:qd/FX8ZBD6n1sVNQO3aIdRxeu5LGw9WhKnYhIIoC2A4=
github.com/cli/safeexec v1.0.0/go.mod h1:Z/D4tTN8Vs5gXYHDCbaM1S/anmEDnJb1iW0+EJ5zx3Q=
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/danieljoos/wincred v1.2.0 h1:ozqKHaLK0W/ii4KVbbvluM91W2H3Sh0BncbUNPS7jLE=
github.com/danieljoos/wincred v1.2.0/go.mod h1:FzQLLMKBFdvu+osBrnFODiv32YGwCfx0SkRa/eYHgec=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc h1:U9qPSI2PIWSS1VwoXQT9A3Wy9MM3WgvqSxFWenqJduM=
@@ -29,6 +33,8 @@ github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f/go.mod h1:vw97
github.com/frankban/quicktest v1.14.6 h1:7Xjx+VpznH+oBnejlPUj8oUpdxnVs4f8XU8WnHkI4W8=
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
github.com/godbus/dbus/v5 v5.1.0 h1:4KLkAxT3aOY8Li4FRJe/KvhoNFFxo0m6fNuFUO8QJUk=
github.com/godbus/dbus/v5 v5.1.0/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4=
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
@@ -89,6 +95,7 @@ github.com/spf13/viper v1.19.0/go.mod h1:GQUN9bilAbhU/jgc1bKs99f/suXKeUMct8Adx5+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY=
github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
@@ -99,6 +106,8 @@ github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e h1:JVG44RsyaB9T2KIHavMF/ppJZNG9ZpyihvCd0w101no=
github.com/xo/terminfo v0.0.0-20220910002029-abceb7e1c41e/go.mod h1:RbqR21r5mrJuqunuUZ/Dhy/avygyECGrLceyNeo4LiM=
github.com/zalando/go-keyring v0.2.5 h1:Bc2HHpjALryKD62ppdEzaFG6VxL6Bc+5v0LYpN8Lba8=
github.com/zalando/go-keyring v0.2.5/go.mod h1:HL4k+OXQfJUWaMnqyuSOc0drfGPX2b51Du6K+MRgZMk=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
golang.org/x/exp v0.0.0-20240613232115-7f521ea00fb8 h1:yixxcjnhBmY0nkL253HFVIm0JsFHwrHdT3Yh6szTnfY=