mirror of
https://gitlab.durp.info/durfy/apps/durpcli.git
synced 2026-05-07 16:00:35 -05:00
update
This commit is contained in:
parent
d96fae1276
commit
125e0dc9cf
9 changed files with 167 additions and 83 deletions
63
cmd/auth/login.go
Normal file
63
cmd/auth/login.go
Normal 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)
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue