Skip to content

Commit

Permalink
Add more SDK methods (#13)
Browse files Browse the repository at this point in the history
* Add more SDK methods

* Add more SDK methods

* client info fix

* Expose credential exists method

* add error data to server error
  • Loading branch information
corbadovych authored Dec 5, 2023
1 parent b39519d commit 6c76622
Show file tree
Hide file tree
Showing 22 changed files with 933 additions and 68 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ The SDK supports Go version 1.18 and above.
## Usage

```
$ go get github.com/corbado/corbado-go@v0.4.2
$ go get github.com/corbado/corbado-go@v0.5.0
```

Import SDK in your Go files:
Expand Down
75 changes: 75 additions & 0 deletions pkg/sdk/emailcode/emailcodes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package emailcode

import (
"context"

"github.com/corbado/corbado-go/pkg/sdk/assert"
"github.com/corbado/corbado-go/pkg/sdk/entity/api"
"github.com/corbado/corbado-go/pkg/sdk/entity/common"
"github.com/corbado/corbado-go/pkg/sdk/servererror"
)

type EmailCode interface {
Send(ctx context.Context, req api.EmailCodeSendReq, editors ...api.RequestEditorFn) (*api.EmailCodeSendRsp, error)
Validate(ctx context.Context, emailCodeID common.EmailCodeID, req api.EmailCodeValidateReq, editors ...api.RequestEditorFn) (*api.EmailCodeValidateRsp, error)
Get(ctx context.Context, emailCodeID common.EmailCodeID, editors ...api.RequestEditorFn) (*api.EmailCodeGetRsp, error)
}

type Impl struct {
client *api.ClientWithResponses
}

var _ EmailCode = &Impl{}

// New returns new email code client
func New(client *api.ClientWithResponses) (*Impl, error) {
if err := assert.NotNil(client); err != nil {
return nil, err
}

return &Impl{
client: client,
}, nil
}

// Send sends email code email to given email address
func (i *Impl) Send(ctx context.Context, req api.EmailCodeSendReq, editors ...api.RequestEditorFn) (*api.EmailCodeSendRsp, error) {
res, err := i.client.EmailCodeSendWithResponse(ctx, req, editors...)
if err != nil {
return nil, err
}

if res.JSONDefault != nil {
return nil, servererror.New(res.JSONDefault)
}

return res.JSON200, nil
}

// Validate validates email code token
func (i *Impl) Validate(ctx context.Context, emailCodeID common.EmailCodeID, req api.EmailCodeValidateReq, editors ...api.RequestEditorFn) (*api.EmailCodeValidateRsp, error) {
res, err := i.client.EmailCodeValidateWithResponse(ctx, emailCodeID, req, editors...)
if err != nil {
return nil, err
}

if res.JSONDefault != nil {
return nil, servererror.New(res.JSONDefault)
}

return res.JSON200, nil
}

// Get gets email code
func (i *Impl) Get(ctx context.Context, emailCodeID common.EmailCodeID, editors ...api.RequestEditorFn) (*api.EmailCodeGetRsp, error) {
res, err := i.client.EmailCodeGetWithResponse(ctx, emailCodeID, editors...)
if err != nil {
return nil, err
}

if res.JSONDefault != nil {
return nil, servererror.New(res.JSONDefault)
}

return res.JSON200, nil
}
207 changes: 143 additions & 64 deletions pkg/sdk/entity/api/api.gen.go

Large diffs are not rendered by default.

13 changes: 13 additions & 0 deletions pkg/sdk/entity/common/common.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

193 changes: 193 additions & 0 deletions pkg/sdk/passkey/passkeys.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
package passkey

import (
"context"

"github.com/corbado/corbado-go/pkg/sdk/assert"
"github.com/corbado/corbado-go/pkg/sdk/entity/api"
"github.com/corbado/corbado-go/pkg/sdk/servererror"
)

type Passkey interface {
RegisterStart(ctx context.Context, req api.WebAuthnRegisterStartReq, editors ...api.RequestEditorFn) (*api.WebAuthnRegisterStartRsp, error)
RegisterFinish(ctx context.Context, req api.WebAuthnFinishReq, editors ...api.RequestEditorFn) (*api.WebAuthnRegisterFinishRsp, error)
AuthenticateStart(ctx context.Context, req api.WebAuthnAuthenticateStartReq, editors ...api.RequestEditorFn) (*api.WebAuthnAuthenticateStartRsp, error)
AuthenticateFinish(ctx context.Context, req api.WebAuthnFinishReq, editors ...api.RequestEditorFn) (*api.WebAuthnAuthenticateFinishRsp, error)
MediationStart(ctx context.Context, req api.WebAuthnMediationStartReq, editors ...api.RequestEditorFn) (*api.WebAuthnMediationStartRsp, error)
AssociateStart(ctx context.Context, req api.WebAuthnAssociateStartReq, editors ...api.RequestEditorFn) (*api.WebAuthnAssociateStartRsp, error)
CredentialList(ctx context.Context, params *api.WebAuthnCredentialListParams, editors ...api.RequestEditorFn) (*api.WebAuthnCredentialListRsp, error)
CredentialUpdate(ctx context.Context, credentialID string, req api.WebAuthnCredentialReq, editors ...api.RequestEditorFn) (*api.WebAuthnCredentialRsp, error)
CredentialExists(ctx context.Context, req api.WebAuthnCredentialExistsReq, editors ...api.RequestEditorFn) (*api.WebAuthnCredentialExistsRsp, error)
CredentialDelete(ctx context.Context, userID string, credentialID string, req api.EmptyReq, editors ...api.RequestEditorFn) error
}

type Impl struct {
client *api.ClientWithResponses
}

var _ Passkey = &Impl{}

// New returns new projects client
func New(client *api.ClientWithResponses) (*Impl, error) {
if err := assert.NotNil(client); err != nil {
return nil, err
}

return &Impl{
client: client,
}, nil
}

// CreateSecret creates an API secret
func (i *Impl) CreateSecret(ctx context.Context, req api.ProjectSecretCreateReq, editors ...api.RequestEditorFn) (*api.ProjectSecretCreateRsp, error) {
res, err := i.client.ProjectSecretCreateWithResponse(ctx, req, editors...)
if err != nil {
return nil, err
}

if res.JSONDefault != nil {
return nil, servererror.New(res.JSONDefault)
}

return res.JSON200, nil
}

// RegisterStart starts registration of a user for Passkeys (Biometrics)
func (i *Impl) RegisterStart(ctx context.Context, req api.WebAuthnRegisterStartReq, editors ...api.RequestEditorFn) (*api.WebAuthnRegisterStartRsp, error) {
res, err := i.client.WebAuthnRegisterStartWithResponse(ctx, req, editors...)
if err != nil {
return nil, err
}

if res.JSONDefault != nil {
return nil, servererror.New(res.JSONDefault)
}

return res.JSON200, nil
}

// RegisterFinish completes registration of a user for Passkeys (Biometrics)
func (i *Impl) RegisterFinish(ctx context.Context, req api.WebAuthnFinishReq, editors ...api.RequestEditorFn) (*api.WebAuthnRegisterFinishRsp, error) {
res, err := i.client.WebAuthnRegisterFinishWithResponse(ctx, req, editors...)
if err != nil {
return nil, err
}

if res.JSONDefault != nil {
return nil, servererror.New(res.JSONDefault)
}

return res.JSON200, nil
}

// AuthenticateStart starts authentication of a user for Passkeys (Biometrics)
func (i *Impl) AuthenticateStart(ctx context.Context, req api.WebAuthnAuthenticateStartReq, editors ...api.RequestEditorFn) (*api.WebAuthnAuthenticateStartRsp, error) {
res, err := i.client.WebAuthnAuthenticateStartWithResponse(ctx, req, editors...)
if err != nil {
return nil, err
}

if res.JSONDefault != nil {
return nil, servererror.New(res.JSONDefault)
}

return res.JSON200, nil
}

// AuthenticateFinish completes authentication of a user for Passkeys (Biometrics)
func (i *Impl) AuthenticateFinish(ctx context.Context, req api.WebAuthnFinishReq, editors ...api.RequestEditorFn) (*api.WebAuthnAuthenticateFinishRsp, error) {
res, err := i.client.WebAuthnAuthenticateFinishWithResponse(ctx, req, editors...)
if err != nil {
return nil, err
}

if res.JSONDefault != nil {
return nil, servererror.New(res.JSONDefault)
}

return res.JSON200, nil
}

// MediationStart starts mediation for Passkeys (Biometrics)
func (i *Impl) MediationStart(ctx context.Context, req api.WebAuthnMediationStartReq, editors ...api.RequestEditorFn) (*api.WebAuthnMediationStartRsp, error) {
res, err := i.client.WebAuthnMediationStartWithResponse(ctx, req, editors...)
if err != nil {
return nil, err
}

if res.JSONDefault != nil {
return nil, servererror.New(res.JSONDefault)
}

return res.JSON200, nil
}

// AssociateStart starts association token flow for Passkeys (Biometrics)
func (i *Impl) AssociateStart(ctx context.Context, req api.WebAuthnAssociateStartReq, editors ...api.RequestEditorFn) (*api.WebAuthnAssociateStartRsp, error) {
res, err := i.client.WebAuthnAssociateStartWithResponse(ctx, req, editors...)
if err != nil {
return nil, err
}

if res.JSONDefault != nil {
return nil, servererror.New(res.JSONDefault)
}

return res.JSON200, nil
}

// CredentialList lists webauthn credentials users
func (i *Impl) CredentialList(ctx context.Context, params *api.WebAuthnCredentialListParams, editors ...api.RequestEditorFn) (*api.WebAuthnCredentialListRsp, error) {
res, err := i.client.WebAuthnCredentialListWithResponse(ctx, params, editors...)
if err != nil {
return nil, err
}

if res.JSONDefault != nil {
return nil, servererror.New(res.JSONDefault)
}

return res.JSON200, nil
}

// CredentialUpdate updates webauthn credential
func (i *Impl) CredentialUpdate(ctx context.Context, credentialID string, req api.WebAuthnCredentialReq, editors ...api.RequestEditorFn) (*api.WebAuthnCredentialRsp, error) {
res, err := i.client.WebAuthnCredentialUpdateWithResponse(ctx, credentialID, req, editors...)
if err != nil {
return nil, err
}

if res.JSONDefault != nil {
return nil, servererror.New(res.JSONDefault)
}

return res.JSON200, nil
}

// CredentialExists checks if active webauthn credential exists for provided user and device
func (i *Impl) CredentialExists(ctx context.Context, req api.WebAuthnCredentialExistsReq, editors ...api.RequestEditorFn) (*api.WebAuthnCredentialExistsRsp, error) {
res, err := i.client.WebAuthnCredentialExistsWithResponse(ctx, req, editors...)
if err != nil {
return nil, err
}

if res.JSONDefault != nil {
return nil, servererror.New(res.JSONDefault)
}

return res.JSON200, nil
}

// CredentialDelete deletes webauthn credential
func (i *Impl) CredentialDelete(ctx context.Context, userID string, credentialID string, req api.EmptyReq, editors ...api.RequestEditorFn) error {
res, err := i.client.WebAuthnCredentialDeleteWithResponse(ctx, userID, credentialID, req, editors...)
if err != nil {
return err
}

if res.JSONDefault != nil {
return servererror.New(res.JSONDefault)
}

return nil
}
45 changes: 45 additions & 0 deletions pkg/sdk/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ type Project interface {
CreateSecret(ctx context.Context, req api.ProjectSecretCreateReq, editors ...api.RequestEditorFn) (*api.ProjectSecretCreateRsp, error)
ConfigGet(ctx context.Context, editors ...api.RequestEditorFn) (*api.ProjectConfigGetRsp, error)
ConfigUpdate(ctx context.Context, req api.ProjectConfigSaveReq, editors ...api.RequestEditorFn) error
AuthMethodsList(ctx context.Context, req api.AuthMethodsListReq, editors ...api.RequestEditorFn) (*api.AuthMethodsListRsp, error)
AndroidAppConfigGet(ctx context.Context, editors ...api.RequestEditorFn) (*api.AndroidAppConfigListRsp, error)
IOSAppConfigGet(ctx context.Context, editors ...api.RequestEditorFn) (*api.IOSAppConfigListRsp, error)
}

type Impl struct {
Expand Down Expand Up @@ -72,3 +75,45 @@ func (i *Impl) ConfigUpdate(ctx context.Context, req api.ProjectConfigSaveReq, e

return nil
}

// AuthMethodsList retrieves possible authentication methods for provided username
func (i *Impl) AuthMethodsList(ctx context.Context, req api.AuthMethodsListReq, editors ...api.RequestEditorFn) (*api.AuthMethodsListRsp, error) {
res, err := i.client.AuthMethodsListWithResponse(ctx, req, editors...)
if err != nil {
return nil, err
}

if res.JSONDefault != nil {
return nil, servererror.New(res.JSONDefault)
}

return res.JSON200, nil
}

// AndroidAppConfigGet retrieves Android App Configurations for a project
func (i *Impl) AndroidAppConfigGet(ctx context.Context, editors ...api.RequestEditorFn) (*api.AndroidAppConfigListRsp, error) {
res, err := i.client.AndroidAppConfigGetWithResponse(ctx, editors...)
if err != nil {
return nil, err
}

if res.JSONDefault != nil {
return nil, servererror.New(res.JSONDefault)
}

return res.JSON200, nil
}

// IOSAppConfigGet retrieves iOS App Configurations for a project
func (i *Impl) IOSAppConfigGet(ctx context.Context, editors ...api.RequestEditorFn) (*api.IOSAppConfigListRsp, error) {
res, err := i.client.IOSAppConfigGetWithResponse(ctx, editors...)
if err != nil {
return nil, err
}

if res.JSONDefault != nil {
return nil, servererror.New(res.JSONDefault)
}

return res.JSON200, nil
}
2 changes: 2 additions & 0 deletions pkg/sdk/servererror/servererror.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type ServerError struct {
Links []string `json:"links"`
Type string `json:"type"`
Validation *ValidationErrors
Data *map[string]any

HTTPStatusCode int32 `json:"httpStatusCode"`
Message string `json:"message"`
Expand All @@ -35,6 +36,7 @@ func New(cause *common.ErrorRsp) *ServerError {
Links: cause.Error.Links,
Type: cause.Error.Type,
Validation: cause.Error.Validation,
Data: cause.Data,

HTTPStatusCode: cause.HttpStatusCode,
Message: cause.Message,
Expand Down
Loading

0 comments on commit 6c76622

Please sign in to comment.