-
Notifications
You must be signed in to change notification settings - Fork 1
/
example_test.go
223 lines (201 loc) · 7.3 KB
/
example_test.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
package smartid
import (
"context"
"fmt"
"log"
)
func ExampleGenerateAuthHash() {
hash := GenerateAuthHash(SHA512)
fmt.Println(hash)
// Output: [46 117 219 69 255 193 115 74 0 96 133 66 216 167 99 93 127 89 158 75 218 203 252 240 196 213 171 133 188 200 23 170 70 31 27 209 213 109 225 183 46 78 169 27 148 118 58 120 142 199 100 164 235 69 107 157 219 201 143 1 112 244 171 183]
}
func ExampleAuthHash_ToBase64String() {
hash := GenerateAuthHash(SHA512)
fmt.Println(hash.ToBase64String())
// Output: LnXbRf/Bc0oAYIVC2KdjXX9Znkvay/zwxNWrhbzIF6pGHxvR1W3hty5OqRuUdjp4jsdkpOtFa53byY8BcPSrtw==
}
func ExampleAuthHash_EncodeBase64() {
hash := GenerateAuthHash(SHA512)
fmt.Println(hash.EncodeBase64())
// Output: [76 110 88 98 82 102 47 66 99 48 111 65 89 73 86 67 50 75 100 106 88 88 57 90 110 107 118 97 121 47 122 119 120 78 87 114 104 98 122 73 70 54 112 71 72 120 118 82 49 87 51 104 116 121 53 79 113 82 117 85 100 106 112 52 106 115 100 107 112 79 116 70 97 53 51 98 121 89 56 66 99 80 83 114 116 119 61 61]
}
func ExampleAuthHash_CalculateVerificationCode() {
hash := GenerateAuthHash(SHA512)
fmt.Println(hash.CalculateVerificationCode())
// Output: 3174
}
func ExampleNewSemanticIdentifier() {
semid := NewSemanticIdentifier(IdentifierTypePNO, CountryEE, "12345678901")
fmt.Println(semid)
// Output: PNOEE-12345678901
}
func ExampleClient_AuthenticateSync() {
semid := NewSemanticIdentifier(IdentifierTypePNO, CountryEE, "30303039914")
client := NewClient("https://sid.demo.sk.ee/smart-id-rp/v2/", 5000)
request := AuthRequest{
// Replace in production with real RelyingPartyUUID.
RelyingPartyUUID: "00000000-0000-0000-0000-000000000000",
// Replace in production with real RelyingPartyName.
RelyingPartyName: "DEMO",
// It is good to generate new has for security reasons.
Hash: GenerateAuthHash(SHA512),
// We use personal ID as Identifier, also possible to use document
// number.
Identifier: semid,
AuthType: AuthTypeEtsi,
}
resp, err := client.AuthenticateSync(context.TODO(), &request)
if err != nil {
log.Fatalln(err)
}
if _, err := resp.Validate(); err != nil {
log.Fatalln(err)
}
// It is also good to verify the certificate over secure. But it isn't
// mandatory, but strongly recommended.
//
// If you always get an error: x509: certificate signed by unknown
// authority. Most probably you need install ca-certificates for
// example for GNU Linux.
//
// sudo apt-get install ca-certificates
// sudo dnf install ca-certificates
certPaths := []string{"./certs/TEST_of_EID-SK_2016.pem.crt"}
if ok, err := resp.Cert.Verify(certPaths); !ok {
log.Fatalln(err)
}
identity := resp.GetIdentity()
fmt.Println("Name:", identity.CommonName)
fmt.Println("Personal ID:", identity.SerialNumber)
fmt.Println("Country:", identity.Country)
// Output:
// Name: TESTNUMBER,OK
// Personal ID: PNOEE-30303039914
// Country: EE
}
func ExampleClient_Authenticate() {
semid := NewSemanticIdentifier(IdentifierTypePNO, CountryEE, "30303039914")
client := NewClient("https://sid.demo.sk.ee/smart-id-rp/v2/", 5000)
request := AuthRequest{
// Replace in production with real RelyingPartyUUID.
RelyingPartyUUID: "00000000-0000-0000-0000-000000000000",
// Replace in production with real RelyingPartyName.
RelyingPartyName: "DEMO",
// It is good to generate new has for security reasons.
Hash: GenerateAuthHash(SHA512),
// We use personal ID as Identifier, also possible to use document
// number.
Identifier: semid,
AuthType: AuthTypeEtsi,
}
resp := <-client.Authenticate(context.TODO(), &request)
if _, err := resp.Validate(); err != nil {
log.Fatalln(err)
}
// It is also good to verify the certificate over secure. But it isn't
// mandatory, but strongly recommended.
//
// If you always get an error: x509: certificate signed by unknown
// authority. Most probably you need install ca-certificates for
// example for GNU Linux.
//
// sudo apt-get install ca-certificates
// sudo dnf install ca-certificates
certPaths := []string{"./certs/TEST_of_EID-SK_2016.pem.crt"}
if ok, err := resp.Cert.Verify(certPaths); !ok {
log.Fatalln(err)
}
identity := resp.GetIdentity()
fmt.Println("Name:", identity.CommonName)
fmt.Println("Personal ID:", identity.SerialNumber)
fmt.Println("Country:", identity.Country)
// Output:
// Name: TESTNUMBER,OK
// Personal ID: PNOEE-30303039914
// Country: EE
}
func ExampleClient_SignSync() {
semid := NewSemanticIdentifier(IdentifierTypePNO, CountryEE, "30303039914")
client := NewClient("https://sid.demo.sk.ee/smart-id-rp/v2/", 5000)
request := AuthRequest{
// Replace in production with real RelyingPartyUUID.
RelyingPartyUUID: "00000000-0000-0000-0000-000000000000",
// Replace in production with real RelyingPartyName.
RelyingPartyName: "DEMO",
// It is good to generate new has for security reasons.
Hash: GenerateAuthHash(SHA512),
// We use personal ID as Identifier, also possible to use document
// number.
Identifier: semid,
AuthType: AuthTypeEtsi,
}
resp, err := client.AuthenticateSync(context.TODO(), &request)
if err != nil {
log.Fatalln(err)
}
if _, err := resp.Validate(); err != nil {
log.Fatalln(err)
}
// It is also good to verify the certificate over secure. But it isn't
// mandatory, but strongly recommended.
//
// If you always get an error: x509: certificate signed by unknown
// authority. Most probably you need install ca-certificates for
// example for GNU Linux.
//
// sudo apt-get install ca-certificates
// sudo dnf install ca-certificates
certPaths := []string{"./certs/TEST_of_EID-SK_2016.pem.crt"}
if ok, err := resp.Cert.Verify(certPaths); !ok {
log.Fatalln(err)
}
identity := resp.GetIdentity()
fmt.Println("Name:", identity.CommonName)
fmt.Println("Personal ID:", identity.SerialNumber)
fmt.Println("Country:", identity.Country)
// Output:
// Name: TESTNUMBER,OK
// Personal ID: PNOEE-30303039914
// Country: EE
}
func ExampleClient_Sign() {
semid := NewSemanticIdentifier(IdentifierTypePNO, CountryEE, "30303039914")
client := NewClient("https://sid.demo.sk.ee/smart-id-rp/v2/", 5000)
request := AuthRequest{
// Replace in production with real RelyingPartyUUID.
RelyingPartyUUID: "00000000-0000-0000-0000-000000000000",
// Replace in production with real RelyingPartyName.
RelyingPartyName: "DEMO",
// It is good to generate new has for security reasons.
Hash: GenerateAuthHash(SHA512),
// We use personal ID as Identifier, also possible to use document
// number.
Identifier: semid,
AuthType: AuthTypeEtsi,
}
resp := <-client.Authenticate(context.TODO(), &request)
if _, err := resp.Validate(); err != nil {
log.Fatalln(err)
}
// It is also good to verify the certificate over secure. But it isn't
// mandatory, but strongly recommended.
//
// If you always get an error: x509: certificate signed by unknown
// authority. Most probably you need install ca-certificates for
// example for GNU Linux.
//
// sudo apt-get install ca-certificates
// sudo dnf install ca-certificates
certPaths := []string{"./certs/TEST_of_EID-SK_2016.pem.crt"}
if ok, err := resp.Cert.Verify(certPaths); !ok {
log.Fatalln(err)
}
identity := resp.GetIdentity()
fmt.Println("Name:", identity.CommonName)
fmt.Println("Personal ID:", identity.SerialNumber)
fmt.Println("Country:", identity.Country)
// Output:
// Name: TESTNUMBER,OK
// Personal ID: PNOEE-30303039914
// Country: EE
}