-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.go
68 lines (55 loc) · 1.17 KB
/
user.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
package user
import (
"time"
"github.com/txchat/dtalk/pkg/auth"
xcrypt "github.com/txchat/dtalk/pkg/crypt"
secp256k1_ethereum "github.com/txchat/dtalk/pkg/crypt/secp256k1-ethereum"
)
var (
driver xcrypt.Encrypt
)
func init() {
var err error
driver, err = xcrypt.Load(secp256k1_ethereum.Name)
if err != nil {
panic(err)
}
}
var TokenTimeout = time.Minute * 2
type User struct {
address string
userName string
priKey, pubKey []byte
token string
tokenExpire time.Time
}
func (u *User) genToken() string {
authenticator := auth.NewDefaultApiAuthenticatorAsDriver(driver)
return authenticator.Request("dtalk", u.pubKey, u.priKey)
}
func (u *User) Token() string {
if time.Now().After(u.tokenExpire.Add(TokenTimeout)) {
u.tokenExpire = time.Now()
return u.genToken()
}
return u.token
}
func (u *User) SetUsername(username string) {
u.userName = username
}
func (u *User) GetUsername() string {
if u.userName == "" {
return u.address
}
return u.userName
}
func (u *User) GetUID() string {
return u.address
}
func NewUser(address string, priKey, pubKey []byte) *User {
return &User{
address: address,
priKey: priKey,
pubKey: pubKey,
}
}