From c9ba1dfefb11cad49d4c812fa296a517901f8e96 Mon Sep 17 00:00:00 2001 From: imants gulbis Date: Tue, 18 Mar 2025 13:43:11 +0200 Subject: [PATCH 1/2] mapping errors --- authenticate.go | 38 ++++++++++++++++++++++++++++++++++---- errors.go | 11 +++++++++++ 2 files changed, 45 insertions(+), 4 deletions(-) create mode 100644 errors.go diff --git a/authenticate.go b/authenticate.go index 4afcf62..4c12b25 100644 --- a/authenticate.go +++ b/authenticate.go @@ -2,6 +2,7 @@ package smartid import ( "context" + e "errors" "fmt" "github.com/tab/smartid/internal/errors" @@ -34,11 +35,40 @@ func (e *Error) Error() string { return fmt.Sprintf("authentication failed: %s", e.Code) } +// We must expose some internal error, otherwise +func handleMappedError(err error) error { + if e.Is(err, errors.ErrAuthenticationIsRunning) { + return ErrAuthenticationIsRunning + } else if e.Is(err, errors.ErrSmartIdNoSuitableAccount) { + return ErrSmartIdNoSuitableAccount + } else if e.Is(err, errors.ErrSmartIdMaintenance) { + return ErrSmartIdMaintenance + } + + return err +} + +func handleMappedResponseError(err string) error { + switch err { + case USER_REFUSED, + USER_REFUSED_DISPLAYTEXTANDPIN, + USER_REFUSED_VC_CHOICE, + USER_REFUSED_CONFIRMATIONMESSAGE, + USER_REFUSED_CONFIRMATIONMESSAGE_WITH_VC_CHOICE, + USER_REFUSED_CERT_CHOICE: + return ErrUserRefused + case TIMEOUT: + return ErrTimeout + default: + return fmt.Errorf("%s", err) + } +} + // CreateSession creates authentication session with the Smart-ID provider func (c *client) CreateSession(ctx context.Context, nationalIdentityNumber string) (*Session, error) { session, err := requests.CreateAuthenticationSession(ctx, c.config, nationalIdentityNumber) if err != nil { - return nil, err + return nil, handleMappedError(err) } return (*Session)(session), nil @@ -48,12 +78,12 @@ func (c *client) CreateSession(ctx context.Context, nationalIdentityNumber strin func (c *client) FetchSession(ctx context.Context, sessionId string) (*Person, error) { response, err := requests.FetchAuthenticationSession(ctx, c.config, sessionId) if err != nil { - return nil, err + return nil, handleMappedError(err) } switch response.State { case Running: - return nil, errors.ErrAuthenticationIsRunning + return nil, ErrAuthenticationIsRunning case Complete: switch response.Result.EndResult { case OK: @@ -76,7 +106,7 @@ func (c *client) FetchSession(ctx context.Context, sessionId string) (*Person, e USER_REFUSED_CERT_CHOICE, WRONG_VC, TIMEOUT: - return nil, &Error{Code: response.Result.EndResult} + return nil, handleMappedResponseError(response.Result.EndResult) } default: return nil, errors.ErrUnsupportedState diff --git a/errors.go b/errors.go new file mode 100644 index 0000000..26803a8 --- /dev/null +++ b/errors.go @@ -0,0 +1,11 @@ +package smartid + +import "errors" + +var ( + ErrAuthenticationIsRunning = errors.New("authentication is still running") + ErrSmartIdNoSuitableAccount = errors.New("no suitable account of requested type found") + ErrSmartIdMaintenance = errors.New("system is under maintenance, retry again later") + ErrUserRefused = errors.New("user refused") + ErrTimeout = errors.New("user didn't respond in time") +) From 4c195640d19bdec6dfee7f9301d5d367bba8eb2b Mon Sep 17 00:00:00 2001 From: imants gulbis Date: Wed, 19 Mar 2025 10:47:19 +0200 Subject: [PATCH 2/2] 404 error --- internal/requests/requests.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/requests/requests.go b/internal/requests/requests.go index 7e1a4a7..9423682 100644 --- a/internal/requests/requests.go +++ b/internal/requests/requests.go @@ -101,7 +101,7 @@ func CreateAuthenticationSession( }, nil case http.StatusForbidden: return nil, errors.ErrSmartIdAccessForbidden - case StatusNoSuitableAccount: + case StatusNoSuitableAccount, http.StatusNotFound: return nil, errors.ErrSmartIdNoSuitableAccount case StatusViewSmartIdApp: return nil, errors.ErrSmartIdViewApp