Skip to content

Commit

Permalink
Merge pull request #31 from corbado/feat/update-bapi-to-v2
Browse files Browse the repository at this point in the history
Feature: Update BAPI to v2
  • Loading branch information
corbadoman authored Sep 24, 2024
2 parents 76749df + 822b67e commit 7e52a02
Show file tree
Hide file tree
Showing 70 changed files with 5,430 additions and 18,026 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ name: tests

on:
push:
branches: '*'
# Matches all branches including / in name
branches: '**'

concurrency:
group: ${{ github.ref }}
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/ci_integrationtests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ jobs:

- name: Run integration tests
env:
CORBADO_BACKEND_API: ${{ secrets.CORBADO_BACKEND_API }}
CORBADO_PROJECT_ID: ${{ secrets.CORBADO_PROJECT_ID }}
CORBADO_API_SECRET: ${{ secrets.CORBADO_API_SECRET }}
CORBADO_FRONTEND_API: ${{ secrets.CORBADO_FRONTEND_API }}
CORBADO_BACKEND_API: ${{ secrets.CORBADO_BACKEND_API }}
run: go test -tags=integration ./tests/integration/...
2 changes: 1 addition & 1 deletion .github/workflows/ci_unittests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ jobs:
cache: true

- name: Run unit tests
run: go test ./...
run: go test ./tests/unit/...
26 changes: 11 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
[![documentation](https://img.shields.io/badge/documentation-Corbado_Backend_API_Reference-blue.svg)](https://apireference.cloud.corbado.io/backendapi/)
[![Slack](https://img.shields.io/badge/slack-join%20chat-brightgreen.svg)](https://join.slack.com/t/corbado/shared_invite/zt-1b7867yz8-V~Xr~ngmSGbt7IA~g16ZsQ)

The [Corbado](https://www.corbado.com) Go SDK provides convenient access to the [Corbado Backend API](https://apireference.cloud.corbado.io/backendapi/) from applications written in the Go language.
The [Corbado](https://www.corbado.com) Go SDK provides convenient access to the [Corbado Backend API](https://apireference.cloud.corbado.io/backendapi-v2/) from applications written in the Go language.

:warning: 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.

Expand All @@ -26,12 +26,12 @@ The [Corbado](https://www.corbado.com) Go SDK provides convenient access to the
Use the following command to install the Corbado Go SDK:

```bash
go get github.com/corbado/corbado-go@v1.0.3
go get github.com/corbado/corbado-go@v2.0.0
```

### 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](https://app.corbado.com).
To create a Corbado Go SDK instance you need to provide your `Project ID`, `API secret` , `Frontend API` and `Backend API` URLs which can be found at the [Developer Panel](https://app.corbado.com).

```Go
package main
Expand All @@ -41,7 +41,7 @@ import (
)

func main() {
config, err := corbado.NewConfig("<Project ID>", "<API secret>")
config, err := corbado.NewConfig("<Project ID>", "<API secret>", "<Frontend API>", "<Backend API>")
if err != nil {
panic(err)
}
Expand All @@ -61,18 +61,14 @@ A list of examples can be found in the [examples](/examples) directory. [Integra

The Corbado Go SDK provides the following services:

- `AuthTokens` for managing authentication tokens needed for own session management ([examples](tests/integration/authtoken))
- `EmailMagicLinks` for managing email magic links ([examples](tests/integration/emailmagiclink))
- `EmailOTPs` for managing email OTPs ([examples](tests/integration/emailotp))
- `Sessions` for managing sessions ([examples](examples/sessionstdlib))
- `SmsOTPs` for managing SMS OTPs ([examples](tests/integration/smsotp))
- `Sessions` for managing sessions ([examples](examples/stdlib/session))
- `Users` for managing users ([examples](tests/integration/user))
- `Validations` for validating email addresses and phone numbers ([examples](tests/integration/validation))
- `Identifiers` for managing identifiers ([examples](tests/integration/identifier))

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

```Go
users, err := sdk.Users().List(context.Background(), nil)
user, err := sdk.Users().Get(context.Background(), "usr-12345679")
if err != nil {
panic(err)
}
Expand All @@ -95,7 +91,7 @@ import (
)

func main() {
config, err := corbado.NewConfig("<Project ID>", "<API secret>")
config, err := corbado.NewConfig("<Project ID>", "<API secret>", "<Frontend API>", "<Backend API>")
if err != nil {
panic(err)
}
Expand All @@ -106,10 +102,10 @@ func main() {
}

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

// Show request ID (can be used in developer panel to look up the full request
Expand All @@ -130,7 +126,7 @@ func main() {
return
}

fmt.Println(user.Data.ID)
fmt.Println(user.userID)
}

```
Expand Down
2 changes: 1 addition & 1 deletion Taskfile.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tasks:
unittests:
desc: Runs unit tests
cmds:
- go test -v ./...
- go test -v ./tests/unit/...

integrationtests:
desc: Runs integration tests
Expand Down
4 changes: 3 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,9 @@ func newClient(config *Config) (*api.ClientWithResponses, error) {
extraOptions = append(extraOptions, config.ExtraClientOptions...)
}

return api.NewClientWithResponses(config.BackendAPI, extraOptions...)
backendServer := config.BackendAPI + "/v2"

return api.NewClientWithResponses(backendServer, extraOptions...)
}

type httpRequestDoer interface {
Expand Down
76 changes: 30 additions & 46 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package corbado

import (
"fmt"
"net/http"
"os"
"time"
Expand All @@ -13,12 +12,11 @@ import (
)

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

JWKSRefreshInterval time.Duration
JWKSRefreshRateLimit time.Duration
Expand All @@ -29,18 +27,15 @@ type Config struct {
}

const (
configDefaultBackendAPI string = "https://backendapi.corbado.io"
configDefaultFrontendAPI string = "https://%s.frontendapi.corbado.io"
configDefaultShortSessionCookieName string = "cbo_short_session"
configDefaultCacheMaxAge = time.Minute
configDefaultCacheMaxAge = time.Minute

configDefaultJWKSRefreshInterval = time.Hour
configDefaultJWKSRefreshRateLimit = 5 * time.Minute
configDefaultJWKSRefreshTimeout = 10 * time.Second
)

// NewConfig returns new config with sane defaults
func NewConfig(projectID string, apiSecret string) (*Config, error) {
func NewConfig(projectID string, apiSecret string, frontendAPI string, backendAPI string) (*Config, error) {
if err := assert.StringNotEmpty(projectID); err != nil {
return nil, err
}
Expand All @@ -49,42 +44,35 @@ func NewConfig(projectID string, apiSecret string) (*Config, error) {
return nil, err
}

return &Config{
ProjectID: projectID,
APISecret: apiSecret,
FrontendAPI: fmt.Sprintf(configDefaultFrontendAPI, projectID),
BackendAPI: configDefaultBackendAPI,
ShortSessionCookieName: configDefaultShortSessionCookieName,
CacheMaxAge: configDefaultCacheMaxAge,
JWKSRefreshInterval: configDefaultJWKSRefreshInterval,
JWKSRefreshRateLimit: configDefaultJWKSRefreshRateLimit,
JWKSRefreshTimeout: configDefaultJWKSRefreshTimeout,
}, nil
}
if err := assert.StringNotEmpty(frontendAPI); err != nil {
return nil, err
}

// MustNewConfig returns new config and panics if projectID or apiSecret are not specified/empty
func MustNewConfig(projectID string, apiSecret string) *Config {
config, err := NewConfig(projectID, apiSecret)
if err != nil {
panic(err)
if err := assert.StringNotEmpty(backendAPI); err != nil {
return nil, err
}

return config
return &Config{
ProjectID: projectID,
APISecret: apiSecret,
FrontendAPI: frontendAPI,
BackendAPI: backendAPI,
CacheMaxAge: configDefaultCacheMaxAge,
JWKSRefreshInterval: configDefaultJWKSRefreshInterval,
JWKSRefreshRateLimit: configDefaultJWKSRefreshRateLimit,
JWKSRefreshTimeout: configDefaultJWKSRefreshTimeout,
}, nil
}

// NewConfigFromEnv returns new config with values from env variables (CORBADO_PROJECT_ID and CORBADO_API_SECRET)
// NewConfigFromEnv created new config be reading the following environment variables: CORBADO_PROJECT_ID,
// CORBADO_API_SECRET, CORBADO_FRONTEND_API and CORBADO_BACKEND_API
func NewConfigFromEnv() (*Config, error) {
projectID := os.Getenv("CORBADO_PROJECT_ID")
if projectID == "" {
return nil, errors.Errorf("Missing env variable CORBADO_PROJECT_ID")
}

apiSecret := os.Getenv("CORBADO_API_SECRET")
if apiSecret == "" {
return nil, errors.Errorf("Missing env variable CORBADO_API_SECRET")
}

return NewConfig(projectID, apiSecret)
return NewConfig(
os.Getenv("CORBADO_PROJECT_ID"),
os.Getenv("CORBADO_API_SECRET"),
os.Getenv("CORBADO_FRONTEND_API"),
os.Getenv("CORBADO_BACKEND_API"),
)
}

func (c *Config) validate() error {
Expand All @@ -104,10 +92,6 @@ func (c *Config) validate() error {
return errors.WithMessage(err, "Invalid BackendAPI given")
}

if err := assert.StringNotEmpty(c.ShortSessionCookieName); err != nil {
return errors.WithMessage(err, "Invalid ShortSessionCookieName given")
}

if err := assert.DurationNotEmpty(c.CacheMaxAge); err != nil {
return errors.WithMessage(err, "Invalid CacheMaxAge given")
}
Expand Down
Loading

0 comments on commit 7e52a02

Please sign in to comment.