Skip to content

scalekit-inc/scalekit-sdk-go

Repository files navigation


Official Go SDK

Scalekit is an Enterprise Authentication Platform purpose built for B2B applications. This Go SDK helps implement Enterprise Capabilities like Single Sign-on via SAML or OIDC in your Golang applications within a few hours.


Pre-requisites

  1. Sign up for a Scalekit account.
  2. Get your env_url, client_id and client_secret from the Scalekit dashboard.

Installation

go get -u github.com/scalekit-inc/scalekit-sdk-go

Usage

Initialize the Scalekit client using the appropriate credentials. Refer code sample below.

import "github.com/scalekit-inc/scalekit-sdk-go"

func main() {
  scalekitClient := scalekit.NewScalekit(
    "<SCALEKIT_ENV_URL>",
    "<SCALEKIT_CLIENT_ID>",
    "<SCALEKIT_CLIENT_SECRET>",
  )

  // Use the sc object to interact with the Scalekit API
  authUrl, _ := scalekitClient.GetAuthorizationUrl(
    "https://acme-corp.com/redirect-uri",
    scalekit.AuthorizationUrlOptions{
      State: "state",
      ConnectionId: "con_123456789",
    },
  )
}

Examples - SSO with Go HTTP Server

Below is a simple code sample that showcases how to implement Single Sign-on using Scalekit SDK

package main

import (
  "fmt"
  "net/http"

  "github.com/scalekit-inc/scalekit-sdk-go"
)

func main() {
  sc := scalekit.NewScalekit(
    "<SCALEKIT_ENV_URL>",
    "<SCALEKIT_CLIENT_ID>",
    "<SCALEKIT_CLIENT_SECRET>",
  )

  redirectUri := "http://localhost:8080/auth/callback"

  // Get the authorization URL and redirect the user to the IdP login page
  http.HandleFunc("/auth/login", func(w http.ResponseWriter, r *http.Request) {
    authUrl, _ := scalekitClient.GetAuthorizationUrl(
      redirectUri,
      scalekit.AuthorizationUrlOptions{
        State: "state",
        ConnectionId: "con_123456789",
      },
    )
    http.Redirect(w, r, authUrl, http.StatusSeeOther)
  })

  // Handle the callback from the Scalekit
  http.HandleFunc("/auth/callback", func(w http.ResponseWriter, r *http.Request) {
    code := r.URL.Query().Get("code")
    state := r.URL.Query().Get("state")

    authResp, _ := scalekitClient.AuthenticateWithCode(code, redirectUri)

    http.SetCookie(w, &http.Cookie{
      Name: "access_token",
      Value: authResp.AccessToken,
    })

    fmt.Fprintf(w, "Access token: %s", authResp.AccessToken)
  })

  fmt.Println("Server started at http://localhost:8080")
  http.ListenAndServe(":8080", nil)
}

Example Apps

Fully functional sample applications written using some popular web application frameworks and Scalekit SDK. Feel free to clone the repo and run them locally

API Reference

Refer to our API reference docs for detailed information about all our API endpoints and their usage.

More Information

License

This project is licensed under the MIT license. See the LICENSE file for more information.