-
Notifications
You must be signed in to change notification settings - Fork 47
/
token.go
65 lines (50 loc) · 1.61 KB
/
token.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
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT License was not distributed with this
// file, you can obtain one at https://opensource.org/licenses/MIT.
//
// Copyright (c) DUSK NETWORK. All rights reserved.
package rpc
import (
"crypto/ed25519"
"encoding/json"
"github.com/dusk-network/dusk-blockchain/pkg/util/nativeutils/hashset"
)
const servicePrefix = "/node.Auth/"
// CreateSessionRoute is the RPC to create a session.
const CreateSessionRoute = servicePrefix + "CreateSession"
// DropSessionRoute is the RPC to create a session.
const DropSessionRoute = servicePrefix + "DropSession"
// StatusRoute is the RPC to inquiry the status of the wallet.
const StatusRoute = servicePrefix + "Status"
// OpenRoutes is the set of RPC that do not require session authentication.
var OpenRoutes = hashset.New()
func init() {
OpenRoutes.Add([]byte(CreateSessionRoute))
OpenRoutes.Add([]byte(StatusRoute))
}
// AuthToken is what we put in the authorization header.
type AuthToken struct {
AccessToken string `json:"access-token"`
Time int64 `json:"time"`
Sig []byte `json:"signature,omitempty"`
}
// AsSignable returns the signable payload of the struct.
func (a *AuthToken) AsSignable() ([]byte, error) {
clone := &AuthToken{
AccessToken: a.AccessToken,
Time: a.Time,
}
return json.Marshal(clone)
}
// Verify the token using ed25519.
func (a *AuthToken) Verify(edPk ed25519.PublicKey) bool {
if a.Sig == nil {
return false
}
payload, err := a.AsSignable()
if err != nil {
// TODO: log
return false
}
return ed25519.Verify(edPk, payload, a.Sig)
}