-
Notifications
You must be signed in to change notification settings - Fork 1
/
requires-cli-user-auth.go
106 lines (88 loc) · 2.46 KB
/
requires-cli-user-auth.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
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Identify authentication and authorization service
//
// Copyright (C) 2020 Alexei Broner
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
package identify
import (
"context"
"flag"
"github.com/akb/go-cli"
"github.com/akb/identify/internal/config"
"github.com/akb/identify/internal/identity"
)
type contextKey string
const identityContextKey = contextKey("identity")
func RequiresCLIUserAuth(wrapped cli.Command) cli.Command {
return &requiresCLIUserAuthCommand{nil, wrapped}
}
type requiresCLIUserAuthCommand struct {
id *string
wrapped cli.Command
}
func (c requiresCLIUserAuthCommand) Help() {
c.wrapped.Help()
}
func (c *requiresCLIUserAuthCommand) Flags(f *flag.FlagSet) {
c.id = f.String("id", "", "your identity")
if b, ok := (interface{})(c.wrapped).(cli.HasFlags); ok {
b.Flags(f)
}
}
func (c requiresCLIUserAuthCommand) Command(ctx context.Context, args []string, s cli.System) error {
dbPath, err := config.GetDBPath(s)
if err != nil {
return err
}
store, err := identity.NewLocalStore(dbPath)
if err != nil {
return err
}
public, err := store.GetIdentity(*c.id)
store.Close()
if err != nil {
return err
}
s.Print("Passphrase: ")
passphrase, err := s.ReadPassword()
s.Println()
if err != nil {
return err
}
private, err := public.Authenticate(passphrase)
if err != nil {
return err
}
ctx = context.WithValue(ctx, identityContextKey, private)
if b, ok := (interface{})(c.wrapped).(cli.Action); ok {
return b.Command(ctx, args, s)
} else {
c.Help()
return nil
}
}
func (c requiresCLIUserAuthCommand) Subcommands() cli.CLI {
if b, ok := (interface{})(c.wrapped).(cli.HasSubcommands); ok {
return b.Subcommands()
}
return nil
}
func IdentityFromContext(ctx context.Context) identity.PrivateIdentity {
if v := ctx.Value(identityContextKey); v != nil {
if p, ok := v.(identity.PrivateIdentity); ok {
return p
}
}
return nil
}