forked from DNAProject/DNA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
state.go
189 lines (171 loc) · 4.08 KB
/
state.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
* Copyright (C) 2018 The DNA Authors
* This file is part of The DNA library.
*
* The DNA is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* The DNA 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with The DNA. If not, see <http://www.gnu.org/licenses/>.
*/
package auth
import (
"io"
"github.com/dnaproject2/DNA/common/serialization"
)
/*
* each role is assigned an array of funcNames
*/
type roleFuncs struct {
funcNames []string
}
func (this *roleFuncs) Serialize(w io.Writer) error {
if err := serialization.WriteUint32(w, uint32(len(this.funcNames))); err != nil {
return err
}
for _, fn := range this.funcNames {
if err := serialization.WriteString(w, fn); err != nil {
return err
}
}
return nil
}
func (this *roleFuncs) Deserialize(rd io.Reader) error {
var err error
fnLen, err := serialization.ReadUint32(rd)
if err != nil {
return err
}
this.funcNames = make([]string, 0)
for i := uint32(0); i < fnLen; i++ {
fn, err := serialization.ReadString(rd)
if err != nil {
return err
}
this.funcNames = append(this.funcNames, fn)
}
return nil
}
type AuthToken struct {
role []byte
expireTime uint32
level uint8
}
func (this *AuthToken) Serialize(w io.Writer) error {
if err := serialization.WriteVarBytes(w, this.role); err != nil {
return err
}
if err := serialization.WriteUint32(w, this.expireTime); err != nil {
return err
}
if err := serialization.WriteUint8(w, this.level); err != nil {
return err
}
return nil
}
func (this *AuthToken) Deserialize(rd io.Reader) error {
//rd := bytes.NewReader(data)
var err error
this.role, err = serialization.ReadVarBytes(rd)
if err != nil {
return err
}
this.expireTime, err = serialization.ReadUint32(rd)
if err != nil {
return err
}
this.level, err = serialization.ReadUint8(rd)
if err != nil {
return err
}
return nil
}
type DelegateStatus struct {
root []byte
AuthToken
}
func (this *DelegateStatus) Serialize(w io.Writer) error {
if err := serialization.WriteVarBytes(w, this.root); err != nil {
return err
}
if err := this.AuthToken.Serialize(w); err != nil {
return err
}
return nil
}
func (this *DelegateStatus) Deserialize(rd io.Reader) error {
var err error
this.root, err = serialization.ReadVarBytes(rd)
if err != nil {
return err
}
err = this.AuthToken.Deserialize(rd)
return err
}
type Status struct {
status []*DelegateStatus
}
func (this *Status) Serialize(w io.Writer) error {
if err := serialization.WriteUint32(w, uint32(len(this.status))); err != nil {
return err
}
for _, s := range this.status {
if err := s.Serialize(w); err != nil {
return err
}
}
return nil
}
func (this *Status) Deserialize(rd io.Reader) error {
sLen, err := serialization.ReadUint32(rd)
if err != nil {
return err
}
this.status = make([]*DelegateStatus, 0)
for i := uint32(0); i < sLen; i++ {
s := new(DelegateStatus)
err = s.Deserialize(rd)
if err != nil {
return err
}
this.status = append(this.status, s)
}
return nil
}
type roleTokens struct {
tokens []*AuthToken
}
func (this *roleTokens) Serialize(w io.Writer) error {
if err := serialization.WriteUint32(w, uint32(len(this.tokens))); err != nil {
return err
}
for _, token := range this.tokens {
if err := token.Serialize(w); err != nil {
return err
}
}
return nil
}
func (this *roleTokens) Deserialize(rd io.Reader) error {
tLen, err := serialization.ReadUint32(rd)
if err != nil {
return err
}
this.tokens = make([]*AuthToken, 0)
for i := uint32(0); i < tLen; i++ {
tok := new(AuthToken)
err = tok.Deserialize(rd)
if err != nil {
return err
}
this.tokens = append(this.tokens, tok)
}
return nil
}