forked from ory/hydra
/
account.go
60 lines (53 loc) · 1.42 KB
/
account.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
57
58
59
60
package handler
import (
"fmt"
"github.com/codegangsta/cli"
"github.com/howeyc/gopass"
"github.com/ory-am/hydra/account"
"github.com/pborman/uuid"
)
type Account struct {
Ctx *Context
}
func getPassword() (password string) {
fmt.Println("Password: ")
password = string(gopass.GetPasswd())
if password == "" {
fmt.Println("You did not provide a password. Please try again.")
return getPassword()
}
fmt.Println("Confirm password: ")
if password != string(gopass.GetPasswd()) {
fmt.Println("Password and confirmation do not match. Please try again.")
return getPassword()
}
return
}
func (c *Account) Create(ctx *cli.Context) error {
username := ctx.Args().First()
if username == "" {
return fmt.Errorf("Please provide an username.")
}
password := ctx.String("password")
if password == "" {
password = getPassword()
}
c.Ctx.Start()
user, err := c.Ctx.Accounts.Create(account.CreateAccountRequest{
ID: uuid.New(),
Username: username,
Password: password,
Data: "{}",
})
if err != nil {
return fmt.Errorf("Could not create account because %s", err)
}
fmt.Printf(`Created account as "%s".`+"\n", user.GetID())
if ctx.Bool("as-superuser") {
if err := c.Ctx.Policies.Create(superUserPolicy(user.GetID())); err != nil {
return fmt.Errorf("Could not create policy for account because %s", err)
}
fmt.Printf(`Granted superuser privileges to account "%s".`+"\n", user.GetID())
}
return nil
}