corbado

package module
v1.0.6 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2024 License: MIT Imports: 25 Imported by: 0

README ¶

GitHub Repo Cover

Corbado Go SDK

Go Reference Test Status Go Report Card documentation Slack

The Corbado Go SDK provides convenient access to the Corbado Backend API from applications written in the Go language.

âš  The Corbado Go SDK is commonly referred to as a private client, specifically designed for usage within closed backend applications. This particular SDK should exclusively be utilized in such environments, as it is crucial to ensure that the API secret remains strictly confidential and is never shared.

🚀 Getting started | 🛠 Services | 📚 Advanced | 💬 Support & Feedback

🚀 Getting started

Requirements
  • Go 1.18 or later
Installation

Use the following command to install the Corbado Go SDK:

go get github.com/corbado/corbado-go@v1.0.3
Usage

To create a Corbado Go SDK instance you need to provide your Project ID and API secret which can be found at the Developer Panel.

package main

import (
    "github.com/corbado/corbado-go"
)

func main() {
    config, err := corbado.NewConfig("<Project ID>", "<API secret>")
    if err != nil {
        panic(err)
    }

    sdk, err := corbado.NewSDK(config)
    if err != nil {
        panic(err)
    }
}
Examples

A list of examples can be found in the examples directory. Integration tests are good examples as well.

🛠 Services

The Corbado Go SDK provides the following services:

  • AuthTokens for managing authentication tokens needed for own session management (examples)
  • EmailMagicLinks for managing email magic links (examples)
  • EmailOTPs for managing email OTPs (examples)
  • Sessions for managing sessions (examples)
  • SmsOTPs for managing SMS OTPs (examples)
  • Users for managing users (examples)
  • Validations for validating email addresses and phone numbers (examples)

To use a specific service, such as Users, invoke it as shown below:

users, err := sdk.Users().List(context.Background(), nil)
if err != nil {
    panic(err)
}

📚 Advanced

Error handling

The Corbado Go SDK uses Go standard error handling (error interface). If the Backend API returns a HTTP status code other than 200, the Corbado Go SDK returns a ServerError error (which implements the error interface):

package main

import (
    "context"
    "fmt"

    "github.com/corbado/corbado-go"
)

func main() {
    config, err := corbado.NewConfig("<Project ID>", "<API secret>")
    if err != nil {
        panic(err)
    }

    sdk, err := corbado.NewSDK(config)
    if err != nil {
        panic(err)
    }

    // Try to get non-existing user with ID 'usr-123456789'
    user, err := sdk.Users().Get(context.Background(), "usr-123456789", nil)
    if err != nil {
        if serverErr := corbado.AsServerError(err); serverErr != nil {
            // Show HTTP status code (404 in this case)
            fmt.Println(serverErr.HTTPStatusCode)

            // Show request ID (can be used in developer panel to look up the full request
            // and response, see https://app.corbado.com/app/logs/requests)
            fmt.Println(serverErr.RequestData.RequestID)

            // Show runtime of request in seconds (server side)
            fmt.Println(serverErr.Runtime)

            // Show validation error messages (server side validation in case of HTTP
            // status code 400 (Bad Request))
            fmt.Printf("%+v\n", serverErr.Validation)
        } else {
            // Handle other errors
            panic(err)
        }

        return
    }

    fmt.Println(user.Data.ID)
}

💬 Support & Feedback

Report an issue

If you encounter any bugs or have suggestions, please open an issue.

Slack channel

Join our Slack channel to discuss questions or ideas with the Corbado team and other developers.

Slack

Email

You can also reach out to us via email at vincent.delitz@corbado.com.

Vulnerability reporting

Please report suspected security vulnerabilities in private to security@corbado.com. Please do NOT create publicly viewable issues for suspected security vulnerabilities.

Documentation ¶

Index ¶

Constants ¶

View Source
const Version = "1.0.4"

Variables ¶

This section is empty.

Functions ¶

func AsServerError ¶

func AsServerError(err error) *servererror.ServerError

AsServerError casts given error into a ServerError, if possible

func IsServerError ¶

func IsServerError(err error) bool

IsServerError checks if given error is a ServerError

func NewLoggingClientOption ¶

func NewLoggingClientOption() api.ClientOption

NewLoggingClientOption enhances HTTP client to log requests/responses

Types ¶

type Config ¶

type Config struct {
	ProjectID              string
	APISecret              string
	FrontendAPI            string
	BackendAPI             string
	ShortSessionCookieName string
	CacheMaxAge            time.Duration

	JWKSRefreshInterval  time.Duration
	JWKSRefreshRateLimit time.Duration
	JWKSRefreshTimeout   time.Duration

	HTTPClient         *http.Client
	ExtraClientOptions []api.ClientOption
}

func MustNewConfig ¶

func MustNewConfig(projectID string, apiSecret string) *Config

MustNewConfig returns new config and panics if projectID or apiSecret are not specified/empty

func NewConfig ¶

func NewConfig(projectID string, apiSecret string) (*Config, error)

NewConfig returns new config with sane defaults

func NewConfigFromEnv ¶ added in v1.0.0

func NewConfigFromEnv() (*Config, error)

NewConfigFromEnv returns new config with values from env variables (CORBADO_PROJECT_ID and CORBADO_API_SECRET)

type Impl ¶

type Impl struct {
	HTTPClient *http.Client
	// contains filtered or unexported fields
}

func NewSDK ¶

func NewSDK(config *Config) (*Impl, error)

NewSDK returns new SDK

func (*Impl) AuthTokens ¶

func (i *Impl) AuthTokens() authtoken.AuthToken

AuthTokens returns auth tokens client

func (i *Impl) EmailMagicLinks() emailmagiclink.EmailMagicLink

EmailMagicLinks returns email magic links client

func (*Impl) EmailOTPs ¶ added in v1.0.0

func (i *Impl) EmailOTPs() emailotp.EmailOTP

EmailOTPs returns email OTPs client

func (*Impl) Passkeys ¶ added in v0.5.0

func (i *Impl) Passkeys() passkey.Passkey

Passkeys returns passkeys client

func (*Impl) Projects ¶ added in v0.2.0

func (i *Impl) Projects() project.Project

Projects returns projects client

func (*Impl) Sessions ¶

func (i *Impl) Sessions() session.Session

Sessions returns sessions client

func (*Impl) SmsOTPs ¶ added in v1.0.0

func (i *Impl) SmsOTPs() smsotp.SmsOTP

SmsOTPs returns sms OTPs client

func (*Impl) Templates ¶ added in v0.3.0

func (i *Impl) Templates() template.Template

Templates returns templates client

func (*Impl) Users ¶

func (i *Impl) Users() user.User

Users returns users client

func (*Impl) Validations ¶

func (i *Impl) Validations() validation.Validation

Validations returns validation client

type SDK ¶

type SDK interface {
	AuthTokens() authtoken.AuthToken
	EmailMagicLinks() emailmagiclink.EmailMagicLink
	EmailOTPs() emailotp.EmailOTP
	Passkeys() passkey.Passkey
	Projects() project.Project
	Sessions() session.Session
	SmsOTPs() smsotp.SmsOTP
	Templates() template.Template
	Users() user.User
	Validations() validation.Validation
}

Directories ¶

Path Synopsis
examples
basic command
stdlib/session command
internal
pkg
generated/api
Package api provides primitives to interact with the openapi HTTP API.
Package api provides primitives to interact with the openapi HTTP API.
generated/common
Package common provides primitives to interact with the openapi HTTP API.
Package common provides primitives to interact with the openapi HTTP API.

Jump to

Keyboard shortcuts

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