mobileid

package module
v0.3.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 29, 2025 License: MIT Imports: 15 Imported by: 0

README

Go Mobile-ID client

Golang client for the Mobile-ID API (https://www.mobile-id.lt/en/). It is a simple wrapper around the API, which helps easily integrate Mobile-ID authentication into Golang applications.

Features

  • Flexible client configuration
  • Concurrent processing
  • Optional TLS configuration (certificate pinning)

Installation

Use go get to install the package

go get -u github.com/tab/mobileid

Usage

Creating a Client

Create a new client using NewClient() and customize its configuration using chainable methods.

package main

import (
  "context"
  "log"
  "time"

  "github.com/tab/mobileid"
)

func main() {
  client := mobileid.NewClient().
    WithRelyingPartyName("DEMO").
    WithRelyingPartyUUID("00000000-0000-0000-0000-000000000000").
    WithHashType("SHA512").
    WithText("Enter PIN1").
    WithTextFormat("GSM-7").
    WithLanguage("ENG").
    WithURL("https://tsp.demo.sk.ee/mid-api").
    WithTimeout(60 * time.Second)

  if err := client.Validate(); err != nil {
    log.Fatal("Invalid configuration:", err)
  }

  // Further processing...
}
Start Authentication

Initiate a new authentication session with the Mobile-ID provider by calling CreateSession. This function generates a random hash, constructs the session request, and returns a session that includes an identifier and a verification code.

func main() {
  // Create a client...


  phoneNumber := "+37268000769"
  identity := "60001017869"

  session, err := client.CreateSession(context.Background(), phoneNumber, identity)
  if err != nil {
    log.Fatal("Error creating session:", err)
  }

  fmt.Println("Session created:", session)
}
Fetch Session
func main() {
  // Create a client...

  person, err := client.FetchSession(context.Background(), sessionId)
  if err != nil {
    log.Fatal("Error fetching session:", err)
  }

  fmt.Println("Session status:", session.State)
}
Async Example

For applications requiring the processing of multiple authentication sessions simultaneously, Mobile-ID provides a worker model. Create a worker using NewWorker, configure its concurrency and queue size, and then start processing.

package main

import (
  "context"
  "fmt"
  "sync"
  "time"

  "github.com/tab/mobileid"
)

func main() {
  client := mobileid.NewClient().
    WithRelyingPartyName("DEMO").
    WithRelyingPartyUUID("00000000-0000-0000-0000-000000000000").
    WithHashType("SHA512").
    WithText("Enter PIN1").
    WithTextFormat("GSM-7").
    WithLanguage("ENG").
    WithURL("https://tsp.demo.sk.ee/mid-api").
    WithTimeout(60 * time.Second)

  identities := map[string]string{
    "51307149560": "+37269930366",
    "60001017869": "+37268000769",
    "60001018800": "+37200000566",
    "60001019939": "+37200000266",
    "60001019947": "+37207110066",
    "60001019950": "+37201100266",
    "60001019961": "+37200000666",
    "60001019972": "+37201200266",
    "60001019983": "+37213100266",
    "50001018908": "+37266000266",
  }

  worker := mobileid.NewWorker(client).
    WithConcurrency(50).
    WithQueueSize(100)

  ctx := context.Background()

  worker.Start(ctx)
  defer worker.Stop()

  var wg sync.WaitGroup

  for identity, phoneNumber := range identities {
    wg.Add(1)

    session, err := client.CreateSession(ctx, phoneNumber, identity)
    if err != nil {
      fmt.Println("Error creating session:", err)
      wg.Done()
    }
    fmt.Println("Session created:", session)

    resultCh := worker.Process(ctx, session.Id)
    go func() {
      defer wg.Done()
      result := <-resultCh
      if result.Err != nil {
        fmt.Println("Error fetching session:", result.Err)
      } else {
        fmt.Println("Fetched person:", result.Person)
      }
    }()
  }

  wg.Wait()
}

Certificate pinning (optional)

package main

import (
  "context"
  "fmt"
  "sync"
  "time"

  "github.com/tab/mobileid"
)

func main() {
  manager, err := mobileid.NewCertificateManager("./certs")
  if err != nil {
    fmt.Println("Failed to create certificate manager:", err)
  }
  tlsConfig := manager.TLSConfig()

  client := mobileid.NewClient().
    WithRelyingPartyName("DEMO").
    WithRelyingPartyUUID("00000000-0000-0000-0000-000000000000").
    WithHashType("SHA512").
    WithText("Enter PIN1").
    WithTextFormat("GSM-7").
    WithLanguage("ENG").
    WithURL("https://tsp.demo.sk.ee/mid-api").
    WithTimeout(60 * time.Second).
    WithTLSConfig(tlsConfig)

  // Further processing...

Documentation

License

Distributed under the MIT License. See LICENSE for more information.

Acknowledgements

Documentation

Overview

Package mobileid is a generated GoMock package.

Package mobileid is a generated GoMock package.

Index

Constants

View Source
const (
	Running  = "RUNNING"
	Complete = "COMPLETE"

	OK                      = "OK"
	NOT_MID_CLIENT          = "NOT_MID_CLIENT"
	USER_CANCELLED          = "USER_CANCELLED"
	SIGNATURE_HASH_MISMATCH = "SIGNATURE_HASH_MISMATCH"
	PHONE_ABSENT            = "PHONE_ABSENT"
	DELIVERY_ERROR          = "DELIVERY_ERROR"
	SIM_ERROR               = "SIM_ERROR"
	TIMEOUT                 = "TIMEOUT"
)
View Source
const (
	Text       = "Enter PIN1"
	TextFormat = "GSM-7"
	Language   = "ENG"
	Timeout    = requests.Timeout
	URL        = "https://tsp.demo.sk.ee/mid-api"
)
View Source
const (
	DefaultConcurrency = 10
	DefaultQueueSize   = 100
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client interface {
	CreateSession(ctx context.Context, phoneNumber, nationalIdentityNumber string) (*Session, error)
	FetchSession(ctx context.Context, sessionId string) (*Person, error)

	WithRelyingPartyName(name string) Client
	WithRelyingPartyUUID(id string) Client
	WithHashType(hashType string) Client
	WithText(text string) Client
	WithTextFormat(format string) Client
	WithLanguage(language string) Client
	WithURL(url string) Client
	WithTimeout(timeout time.Duration) Client
	WithTLSConfig(tlsConfig *tls.Config) Client

	Validate() error
}

func NewClient

func NewClient() Client

type Error

type Error struct {
	Code string
}

Error represents an error from the Mobile-ID provider

func (*Error) Error

func (e *Error) Error() string

Error returns the error message

type Job

type Job struct {
	SessionId string
	ResultCh  chan Result
}

type Manager added in v0.2.0

type Manager struct {
	// contains filtered or unexported fields
}

func NewCertificateManager added in v0.2.0

func NewCertificateManager(certsDir string) (*Manager, error)

NewCertificateManager creates a new certificate manager instance

func (*Manager) TLSConfig added in v0.2.0

func (p *Manager) TLSConfig() *tls.Config

TLSConfig returns a new tls.Config instance with the certificate pinning

func (*Manager) VerifyPeerCertificate added in v0.2.0

func (p *Manager) VerifyPeerCertificate(rawCerts [][]byte, _ [][]*x509.Certificate) error

VerifyPeerCertificate verifies the peer certificate against the pinned certificates

type MockClient

type MockClient struct {
	// contains filtered or unexported fields
}

MockClient is a mock of Client interface.

func NewMockClient

func NewMockClient(ctrl *gomock.Controller) *MockClient

NewMockClient creates a new mock instance.

func (*MockClient) CreateSession

func (m *MockClient) CreateSession(ctx context.Context, phoneNumber, nationalIdentityNumber string) (*Session, error)

CreateSession mocks base method.

func (*MockClient) EXPECT

func (m *MockClient) EXPECT() *MockClientMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockClient) FetchSession

func (m *MockClient) FetchSession(ctx context.Context, sessionId string) (*Person, error)

FetchSession mocks base method.

func (*MockClient) Validate

func (m *MockClient) Validate() error

Validate mocks base method.

func (*MockClient) WithHashType

func (m *MockClient) WithHashType(hashType string) Client

WithHashType mocks base method.

func (*MockClient) WithLanguage

func (m *MockClient) WithLanguage(language string) Client

WithLanguage mocks base method.

func (*MockClient) WithRelyingPartyName

func (m *MockClient) WithRelyingPartyName(name string) Client

WithRelyingPartyName mocks base method.

func (*MockClient) WithRelyingPartyUUID

func (m *MockClient) WithRelyingPartyUUID(id string) Client

WithRelyingPartyUUID mocks base method.

func (*MockClient) WithTLSConfig added in v0.2.0

func (m *MockClient) WithTLSConfig(tlsConfig *tls.Config) Client

WithTLSConfig mocks base method.

func (*MockClient) WithText

func (m *MockClient) WithText(text string) Client

WithText mocks base method.

func (*MockClient) WithTextFormat

func (m *MockClient) WithTextFormat(format string) Client

WithTextFormat mocks base method.

func (*MockClient) WithTimeout

func (m *MockClient) WithTimeout(timeout time.Duration) Client

WithTimeout mocks base method.

func (*MockClient) WithURL

func (m *MockClient) WithURL(url string) Client

WithURL mocks base method.

type MockClientMockRecorder

type MockClientMockRecorder struct {
	// contains filtered or unexported fields
}

MockClientMockRecorder is the mock recorder for MockClient.

func (*MockClientMockRecorder) CreateSession

func (mr *MockClientMockRecorder) CreateSession(ctx, phoneNumber, nationalIdentityNumber any) *gomock.Call

CreateSession indicates an expected call of CreateSession.

func (*MockClientMockRecorder) FetchSession

func (mr *MockClientMockRecorder) FetchSession(ctx, sessionId any) *gomock.Call

FetchSession indicates an expected call of FetchSession.

func (*MockClientMockRecorder) Validate

func (mr *MockClientMockRecorder) Validate() *gomock.Call

Validate indicates an expected call of Validate.

func (*MockClientMockRecorder) WithHashType

func (mr *MockClientMockRecorder) WithHashType(hashType any) *gomock.Call

WithHashType indicates an expected call of WithHashType.

func (*MockClientMockRecorder) WithLanguage

func (mr *MockClientMockRecorder) WithLanguage(language any) *gomock.Call

WithLanguage indicates an expected call of WithLanguage.

func (*MockClientMockRecorder) WithRelyingPartyName

func (mr *MockClientMockRecorder) WithRelyingPartyName(name any) *gomock.Call

WithRelyingPartyName indicates an expected call of WithRelyingPartyName.

func (*MockClientMockRecorder) WithRelyingPartyUUID

func (mr *MockClientMockRecorder) WithRelyingPartyUUID(id any) *gomock.Call

WithRelyingPartyUUID indicates an expected call of WithRelyingPartyUUID.

func (*MockClientMockRecorder) WithTLSConfig added in v0.2.0

func (mr *MockClientMockRecorder) WithTLSConfig(tlsConfig any) *gomock.Call

WithTLSConfig indicates an expected call of WithTLSConfig.

func (*MockClientMockRecorder) WithText

func (mr *MockClientMockRecorder) WithText(text any) *gomock.Call

WithText indicates an expected call of WithText.

func (*MockClientMockRecorder) WithTextFormat

func (mr *MockClientMockRecorder) WithTextFormat(format any) *gomock.Call

WithTextFormat indicates an expected call of WithTextFormat.

func (*MockClientMockRecorder) WithTimeout

func (mr *MockClientMockRecorder) WithTimeout(timeout any) *gomock.Call

WithTimeout indicates an expected call of WithTimeout.

func (*MockClientMockRecorder) WithURL

func (mr *MockClientMockRecorder) WithURL(url any) *gomock.Call

WithURL indicates an expected call of WithURL.

type MockWorker

type MockWorker struct {
	// contains filtered or unexported fields
}

MockWorker is a mock of Worker interface.

func NewMockWorker

func NewMockWorker(ctrl *gomock.Controller) *MockWorker

NewMockWorker creates a new mock instance.

func (*MockWorker) EXPECT

func (m *MockWorker) EXPECT() *MockWorkerMockRecorder

EXPECT returns an object that allows the caller to indicate expected use.

func (*MockWorker) Process

func (m *MockWorker) Process(ctx context.Context, sessionId string) <-chan Result

Process mocks base method.

func (*MockWorker) Start

func (m *MockWorker) Start(ctx context.Context)

Start mocks base method.

func (*MockWorker) Stop

func (m *MockWorker) Stop()

Stop mocks base method.

func (*MockWorker) WithConcurrency

func (m *MockWorker) WithConcurrency(concurrency int) Worker

WithConcurrency mocks base method.

func (*MockWorker) WithQueueSize

func (m *MockWorker) WithQueueSize(size int) Worker

WithQueueSize mocks base method.

type MockWorkerMockRecorder

type MockWorkerMockRecorder struct {
	// contains filtered or unexported fields
}

MockWorkerMockRecorder is the mock recorder for MockWorker.

func (*MockWorkerMockRecorder) Process

func (mr *MockWorkerMockRecorder) Process(ctx, sessionId any) *gomock.Call

Process indicates an expected call of Process.

func (*MockWorkerMockRecorder) Start

func (mr *MockWorkerMockRecorder) Start(ctx any) *gomock.Call

Start indicates an expected call of Start.

func (*MockWorkerMockRecorder) Stop

func (mr *MockWorkerMockRecorder) Stop() *gomock.Call

Stop indicates an expected call of Stop.

func (*MockWorkerMockRecorder) WithConcurrency

func (mr *MockWorkerMockRecorder) WithConcurrency(concurrency any) *gomock.Call

WithConcurrency indicates an expected call of WithConcurrency.

func (*MockWorkerMockRecorder) WithQueueSize

func (mr *MockWorkerMockRecorder) WithQueueSize(size any) *gomock.Call

WithQueueSize indicates an expected call of WithQueueSize.

type Person

type Person struct {
	IdentityNumber string
	PersonalCode   string
	PhoneNumber    string
	FirstName      string
	LastName       string
}

type Result

type Result struct {
	Person *Person
	Err    error
}

type Session

type Session struct {
	Id   string `json:"sessionID"`
	Code string `json:"code"`
}

type Worker

type Worker interface {
	Start(ctx context.Context)
	Stop()
	Process(ctx context.Context, sessionId string) <-chan Result

	WithConcurrency(concurrency int) Worker
	WithQueueSize(size int) Worker
}

func NewWorker

func NewWorker(client Client) Worker

Directories

Path Synopsis
cmd
client command
internal

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL