|
| 1 | +# Usage |
| 2 | + |
| 3 | +## Configure a client |
| 4 | + |
| 5 | +Create a new client using `NewClient()` and customize its configuration using chainable methods. |
| 6 | + |
| 7 | +```go |
| 8 | +package main |
| 9 | + |
| 10 | +import ( |
| 11 | + "context" |
| 12 | + "log" |
| 13 | + "time" |
| 14 | + |
| 15 | + "github.com/tab/mobileid" |
| 16 | +) |
| 17 | + |
| 18 | +func main() { |
| 19 | + client := mobileid.NewClient(). |
| 20 | + WithRelyingPartyName("DEMO"). |
| 21 | + WithRelyingPartyUUID("00000000-0000-0000-0000-000000000000"). |
| 22 | + WithHashType("SHA512"). |
| 23 | + WithText("Enter PIN1"). |
| 24 | + WithTextFormat("GSM-7"). |
| 25 | + WithLanguage("ENG"). |
| 26 | + WithURL("https://tsp.demo.sk.ee/mid-api"). |
| 27 | + WithTimeout(60 * time.Second) |
| 28 | + |
| 29 | + if err := client.Validate(); err != nil { |
| 30 | + log.Fatal("Invalid configuration:", err) |
| 31 | + } |
| 32 | + |
| 33 | + // Further processing... |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +Check client default configuration values: |
| 38 | + |
| 39 | +```go |
| 40 | +const ( |
| 41 | + Text = "Enter PIN1" |
| 42 | + TextFormat = "GSM-7" |
| 43 | + Language = "ENG" |
| 44 | + Timeout = requests.Timeout |
| 45 | + URL = "https://tsp.demo.sk.ee/mid-api" |
| 46 | +) |
| 47 | + |
| 48 | +cfg := &config.Config{ |
| 49 | + HashType: utils.HashTypeSHA512, |
| 50 | + Text: Text, |
| 51 | + TextFormat: TextFormat, |
| 52 | + Language: Language, |
| 53 | + URL: URL, |
| 54 | + Timeout: Timeout, |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +## Start authentication |
| 59 | + |
| 60 | +Initiate a new authentication session with the `Mobile-ID` provider by calling `CreateSession`. |
| 61 | +This function generates a random hash, constructs the session request, and returns a session that includes an identifier and a verification code. |
| 62 | + |
| 63 | +```go |
| 64 | +func main() { |
| 65 | +// Create a client... |
| 66 | + |
| 67 | + |
| 68 | +phoneNumber := "+37268000769" |
| 69 | +identity := "60001017869" |
| 70 | + |
| 71 | +session, err := client.CreateSession(context.Background(), phoneNumber, identity) |
| 72 | +if err != nil { |
| 73 | +log.Fatal("Error creating session:", err) |
| 74 | +} |
| 75 | + |
| 76 | +fmt.Println("Session created:", session) |
| 77 | +} |
| 78 | +``` |
| 79 | + |
| 80 | +## Fetch authentication session |
| 81 | + |
| 82 | +```go |
| 83 | +func main() { |
| 84 | +// Create a client... |
| 85 | + |
| 86 | +person, err := client.FetchSession(context.Background(), sessionId) |
| 87 | +if err != nil { |
| 88 | +log.Fatal("Error fetching session:", err) |
| 89 | +} |
| 90 | + |
| 91 | +fmt.Println("Session status:", session.State) |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +## Async example |
| 96 | + |
| 97 | +For applications requiring the processing of multiple authentication sessions simultaneously, `Mobile-ID` provides a worker model. |
| 98 | +Create a worker using `NewWorker`, configure its concurrency and queue size, and then start processing. |
| 99 | + |
| 100 | +```go |
| 101 | +package main |
| 102 | + |
| 103 | +import ( |
| 104 | + "context" |
| 105 | + "fmt" |
| 106 | + "sync" |
| 107 | + "time" |
| 108 | + |
| 109 | + "github.com/tab/mobileid" |
| 110 | +) |
| 111 | + |
| 112 | +func main() { |
| 113 | + client := mobileid.NewClient(). |
| 114 | + WithRelyingPartyName("DEMO"). |
| 115 | + WithRelyingPartyUUID("00000000-0000-0000-0000-000000000000"). |
| 116 | + WithHashType("SHA512"). |
| 117 | + WithText("Enter PIN1"). |
| 118 | + WithTextFormat("GSM-7"). |
| 119 | + WithLanguage("ENG"). |
| 120 | + WithURL("https://tsp.demo.sk.ee/mid-api"). |
| 121 | + WithTimeout(60 * time.Second) |
| 122 | + |
| 123 | + identities := map[string]string{ |
| 124 | + "51307149560": "+37269930366", |
| 125 | + "60001017869": "+37268000769", |
| 126 | + "60001018800": "+37200000566", |
| 127 | + "60001019939": "+37200000266", |
| 128 | + "60001019947": "+37207110066", |
| 129 | + "60001019950": "+37201100266", |
| 130 | + "60001019961": "+37200000666", |
| 131 | + "60001019972": "+37201200266", |
| 132 | + "60001019983": "+37213100266", |
| 133 | + "50001018908": "+37266000266", |
| 134 | + } |
| 135 | + |
| 136 | + worker := mobileid.NewWorker(client). |
| 137 | + WithConcurrency(50). |
| 138 | + WithQueueSize(100) |
| 139 | + |
| 140 | + ctx := context.Background() |
| 141 | + |
| 142 | + worker.Start(ctx) |
| 143 | + defer worker.Stop() |
| 144 | + |
| 145 | + var wg sync.WaitGroup |
| 146 | + |
| 147 | + for identity, phoneNumber := range identities { |
| 148 | + wg.Add(1) |
| 149 | + |
| 150 | + session, err := client.CreateSession(ctx, phoneNumber, identity) |
| 151 | + if err != nil { |
| 152 | + fmt.Println("Error creating session:", err) |
| 153 | + wg.Done() |
| 154 | + } |
| 155 | + fmt.Println("Session created:", session) |
| 156 | + |
| 157 | + resultCh := worker.Process(ctx, session.Id) |
| 158 | + go func() { |
| 159 | + defer wg.Done() |
| 160 | + result := <-resultCh |
| 161 | + if result.Err != nil { |
| 162 | + fmt.Println("Error fetching session:", result.Err) |
| 163 | + } else { |
| 164 | + fmt.Println("Fetched person:", result.Person) |
| 165 | + } |
| 166 | + }() |
| 167 | + } |
| 168 | + |
| 169 | + wg.Wait() |
| 170 | +} |
| 171 | +``` |
| 172 | + |
| 173 | +## Certificate pinning (optional) |
| 174 | + |
| 175 | +```go |
| 176 | +package main |
| 177 | + |
| 178 | +import ( |
| 179 | + "context" |
| 180 | + "fmt" |
| 181 | + "sync" |
| 182 | + "time" |
| 183 | + |
| 184 | + "github.com/tab/mobileid" |
| 185 | +) |
| 186 | + |
| 187 | +func main() { |
| 188 | + manager, err := mobileid.NewCertificateManager("./certs") |
| 189 | + if err != nil { |
| 190 | + fmt.Println("Failed to create certificate manager:", err) |
| 191 | + } |
| 192 | + tlsConfig := manager.TLSConfig() |
| 193 | + |
| 194 | + client := mobileid.NewClient(). |
| 195 | + WithRelyingPartyName("DEMO"). |
| 196 | + WithRelyingPartyUUID("00000000-0000-0000-0000-000000000000"). |
| 197 | + WithHashType("SHA512"). |
| 198 | + WithText("Enter PIN1"). |
| 199 | + WithTextFormat("GSM-7"). |
| 200 | + WithLanguage("ENG"). |
| 201 | + WithURL("https://tsp.demo.sk.ee/mid-api"). |
| 202 | + WithTimeout(60 * time.Second). |
| 203 | + WithTLSConfig(tlsConfig) |
| 204 | + |
| 205 | + // Further processing... |
| 206 | +``` |
| 207 | +
|
| 208 | +```sh |
| 209 | +Session created: &{b2769811-16de-42f1-a06e-c580d07c1298 5960} |
| 210 | +Fetched person: &{PNOEE-60001017869 60001017869 EID2016 TESTNUMBER} |
| 211 | +``` |
| 212 | +
|
| 213 | +- **b2769811-16de-42f1-a06e-c580d07c1298** – session id |
| 214 | +- **5960** – verification code |
| 215 | +
|
| 216 | +
|
| 217 | +- **PNOEE-60001017869** – formatted identity |
| 218 | +- **60001017869** – personal identification code |
| 219 | +- **EID2016** – person first name |
| 220 | +- **TESTNUMBER** – person last name |
| 221 | +
|
0 commit comments