-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpwdauth.go
More file actions
91 lines (83 loc) · 2.09 KB
/
pwdauth.go
File metadata and controls
91 lines (83 loc) · 2.09 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// An example using password auth.
//
// $ go run examples/pwdauth/pwdauth.go
package main
import (
"context"
"fmt"
"github.com/dpup/prefab"
"github.com/dpup/prefab/errors"
"github.com/dpup/prefab/plugins/auth"
"github.com/dpup/prefab/plugins/auth/pwdauth"
"google.golang.org/grpc/codes"
)
func main() {
s := prefab.New(
prefab.WithPlugin(auth.Plugin()),
prefab.WithPlugin(pwdauth.Plugin(
pwdauth.WithAccountFinder(accountStore{}),
pwdauth.WithHasher(pwdauth.TestHasher), // Doesn't hash passwords.
)),
prefab.WithStaticFiles("/", "./examples/pwdauth/static/"),
)
fmt.Println("")
fmt.Println("Visit http://localhost:8000/ in your browser")
fmt.Println("")
fmt.Println("Then try logging in with one of these email addresses:")
for _, acc := range testAccounts {
fmt.Println(" ", acc.Email)
}
fmt.Println("")
fmt.Println("All accounts have the password 'password'.")
fmt.Println("")
// Start the server.
if err := s.Start(); err != nil {
fmt.Println(err)
}
}
type accountStore struct{}
func (a accountStore) FindAccount(ctx context.Context, email string) (*pwdauth.Account, error) {
for _, acc := range testAccounts {
if acc.Email == email {
return acc, nil
}
}
return nil, errors.Codef(codes.NotFound, "account not found")
}
var testAccounts = []*pwdauth.Account{
{
ID: "1",
Email: "logan@example.com",
Name: "Logan",
EmailVerified: true,
HashedPassword: []byte("password"),
},
{
ID: "2",
Email: "scott@example.com",
Name: "Scott Summers",
EmailVerified: true,
HashedPassword: []byte("password"),
},
{
ID: "3",
Email: "jean@example.com",
Name: "Jean Grey",
EmailVerified: true,
HashedPassword: []byte("password"),
},
{
ID: "4",
Email: "ororo@example.com",
Name: "Ororo Munroe",
EmailVerified: true,
HashedPassword: []byte("password"),
},
{
ID: "5",
Email: "kurt@example.com",
Name: "Kurt Wagner",
EmailVerified: true,
HashedPassword: []byte("password"),
},
}