-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauthenticate.go
75 lines (58 loc) · 1.75 KB
/
authenticate.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
package main
import (
"context"
"errors"
"fmt"
"os"
"path"
"runtime"
"github.com/e-identification/bankid-go/pkg"
"github.com/e-identification/bankid-go/pkg/configuration"
"github.com/e-identification/bankid-go/pkg/payload"
)
func main() {
clientConfiguration := configuration.NewConfiguration(
configuration.TestEnvironment,
&configuration.Pkcs12{Content: loadPkcs12(getResourcePath("certificates/test.p12")), Password: "qwerty123"},
)
bankID, err := pkg.NewBankIDClient(clientConfiguration)
authenticationPayload := payload.AuthenticationPayload{
EndUserIP: "192.168.1.1", UserVisibleData: "To be showed in the BankID application ",
Requirement: &payload.Requirement{PersonalNumber: "201912312392"},
}
httpResponse, err := bankID.Authenticate(context.Background(), &authenticationPayload)
if err != nil {
var apiError *pkg.APIError
if errors.As(err, &apiError) {
fmt.Printf("%s - %s \n", apiError.Details, apiError.ErrorCode)
return
}
fmt.Printf("%#v", err)
return
}
fmt.Println(httpResponse)
// fmt.Println(httpResponse.Collect(context.Background()))
}
func loadPkcs12(path string) []byte {
cert, err := os.ReadFile(path)
if err != nil {
panic(err)
}
return cert
}
// getResourceDirectoryPath returns the full path to the resource directory.
func getResourceDirectoryPath() (directory string, err error) {
_, filename, _, ok := runtime.Caller(0)
if !ok {
return "", fmt.Errorf("could not derive directory path")
}
return fmt.Sprintf("%s/%s", path.Dir(filename), "../pkg/resource"), nil
}
// getResourcePath returns the full path to the resource.
func getResourcePath(path string) (directory string) {
dir, err := getResourceDirectoryPath()
if err != nil {
panic(err)
}
return fmt.Sprintf("%s/%s", dir, path)
}