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

@@ -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()
},
}