-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
88 lines (77 loc) · 1.89 KB
/
main.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
package main
import (
"context"
"encoding/hex"
"encoding/json"
"fmt"
"time"
"github.com/ethereum/go-ethereum/accounts"
"github.com/ethereum/go-ethereum/crypto"
"github.com/pandodao/passport-go/auth"
"github.com/pandodao/passport-go/eip4361"
)
func main() {
ctx := context.Background()
authorizer := auth.New(
[]string{"dapp_id"}, // issuers whitelist, oauth token must be issued by the issuers
[]string{}, // domain whitelist
)
message, signature, err := makeMessage()
if err != nil {
panic(err)
}
user, err := authorizer.AuthorizeWithMvmValidator(
ctx,
&auth.AuthorizationParams{
Method: auth.AuthMethodMvm,
MvmSignedMessage: message,
MvmSignature: signature,
},
func(ctx context.Context, message *eip4361.Message) error {
// verify anything you want
return nil
},
)
if err != nil {
panic(err)
}
bts, _ := json.MarshalIndent(user, "", " ")
fmt.Println(string(bts))
}
func makeMessage() (string, string, error) {
privateKey, err := crypto.GenerateKey()
if err != nil {
return "", "", err
}
var (
address = crypto.PubkeyToAddress(privateKey.PublicKey)
domain = "pando-apps.aspens.rocks"
uri = "https://pando-apps.aspens.rocks"
nonce = "oCxDubPgiNZdE8z71"
issuedAt = time.Now()
expiredAt = issuedAt.Add(time.Hour)
resources = "https://pando-apps.aspens.rocks"
)
message := fmt.Sprintf(`%s wants you to sign in with your Ethereum account:
%s
I accept the ServiceOrg Terms of Service: https://service.invalid/tos
URI: %s
Version: 1
Chain ID: 1
Nonce: %s
Issued At: %s
Expiration Time: %s
Request ID: F369349D-9B66-4367-BAF2-AE9D83E0F9FA
Resources:
- %s`,
domain,
address,
uri,
nonce,
issuedAt.Format(time.RFC3339),
expiredAt.Format(time.RFC3339),
resources,
)
signature, err := crypto.Sign(accounts.TextHash([]byte(message)), privateKey)
return message, "0x" + hex.EncodeToString(signature), err
}