/
oauth.go
56 lines (50 loc) · 1.25 KB
/
oauth.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package cmd
import (
"context"
"fmt"
"github.com/9d4/semaphore/oauth2/models"
"github.com/9d4/semaphore/oauth2/store"
"github.com/spf13/cobra"
jww "github.com/spf13/jwalterweatherman"
"os"
"text/tabwriter"
)
func init() {
rootCmd.AddCommand(oAuthCmd)
oAuthCmd.AddCommand(oAuthAddCmd)
}
var oAuthCmd = &cobra.Command{
Use: "oauth",
Short: "OAuth utilities",
RunE: func(cmd *cobra.Command, args []string) error {
return cmd.Help()
},
}
var oAuthAddCmd = &cobra.Command{
Use: "add [app-id] [app-domain] [app-secret]",
Short: "Add new client app",
Args: cobra.ExactArgs(3),
Run: boot(func(cmd *cobra.Command, args []string, passData *bootData) {
clientStore := store.NewClientStoreRedis(passData.rdb)
err := clientStore.Set(args[0], &models.Client{
ID: args[0],
Secret: args[2],
Domain: args[1],
})
if err != nil {
jww.FATAL.Fatal(err)
return
}
cli, err := clientStore.GetByID(context.Background(), args[0])
if err != nil {
jww.FATAL.Fatal(err)
return
}
tw := tabwriter.NewWriter(os.Stdout, 4, 4, 2, ' ', 0)
fmt.Println("Created!")
fmt.Fprintf(tw, "ClientID\t :%s\n", cli.GetID())
fmt.Fprintf(tw, "Secret\t :%s\n", cli.GetSecret())
fmt.Fprintf(tw, "Domain\t :%s\n", cli.GetDomain())
tw.Flush()
}),
}