Skip to content

Commit

Permalink
Merge pull request #2 from scalekit-inc/refactor
Browse files Browse the repository at this point in the history
Add additional methods and refactoring
  • Loading branch information
nkbhasker authored Jun 24, 2024
2 parents 1e6b727 + 807a452 commit 53ac4c3
Show file tree
Hide file tree
Showing 40 changed files with 490 additions and 237 deletions.
10 changes: 10 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
version: 2
updates:
- package-ecosystem: "npm"
directory: "/"
schedule:
interval: "weekly"
time: "06:00"
timezone: "Asia/Kolkata"
reviewers:
- "dhawani"
92 changes: 81 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,26 +1,29 @@
<p align="center">
<p align="left">
<a href="https://scalekit.com" target="_blank" rel="noopener noreferrer">
<picture>
<img src="https://cdn.scalekit.cloud/v1/scalekit-logo-dark.svg" height="64">
</picture>
</a>
<br/>
</p>
<h1 align="center">
Official Scalekit Node SDK
</h1>

Scalekit helps you in shipping enterprise auth in days.
# Official Node.js SDK
<a href="https://scalekit.com" target="_blank" rel="noopener noreferrer">Scalekit</a> is an Enterprise Authentication Platform purpose built for B2B applications. This Node.js SDK helps implement Enterprise Capabilities like Single Sign-on via SAML or OIDC in your Node.js applications within a few hours.

This Node SDK is a wrapper around Scalekit's REST API to help you integrate Scalekit with your Node.js applications.
<div>
📚 <a target="_blank" href="https://docs.scalekit.com">Documentation</a> - 🚀 <a target="_blank" href="https://docs.scalekit.com">Quick-start Guide</a> - 💻 <a target="_blank" href="https://docs.scalekit.com/apis">API Reference</a>
</div>
<hr />

## Getting Started
## Pre-requisites

1. [Sign up](https://scalekit.com) for a Scalekit account.
2. Get your ```env_url```, ```client_id``` and ```client_secret``` from the Scalekit dashboard.

## Installation

Install Scalekit SDK using your preferred package manager.

```sh
npm install @scalekit-sdk/node
#or
Expand All @@ -31,19 +34,86 @@ pnpm add @scalekit-sdk/node

## Usage

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

```javascript
import { Scalekit } from "@scalekit-sdk/node";
import { ScalekitClient } from "@scalekit-sdk/node";

const scalekit = new Scalekit(
const sc = new ScalekitClient(
process.env.SCALEKIT_ENV_URL!,
process.env.SCALEKIT_CLIENT_ID!,
process.env.SCALEKIT_CLIENT_SECRET!
);

// Use the sc object to interact with the Scalekit API
const authUrl = sc.getAuthorizationUrl("https://acme-corp.com/redirect-uri", {
state: "state",
connectionId: "connection_id",
});

```

## Examples - SSO with Express.js

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

```javascript
import express from "express";
import { ScalekitClient } from "@scalekit-sdk/node";

const app = express();

const sc = new ScalekitClient(
process.env.SCALEKIT_ENV_URL!,
process.env.SCALEKIT_CLIENT_ID!,
process.env.SCALEKIT_CLIENT_SECRET!
);

const redirectUri = `${process.env.HOST}/auth/callback`;

// Get the authorization URL and redirect the user to the IdP login page
app.get("/auth/login", (req, res) => {
const authUrl = sc.getAuthorizationUrl(
redirectUri,
{
state: "state",
connectionId: "connection_id",
}
);

res.redirect(authUrl);
});

// Handle the callback from Scalekit
app.get("/auth/callback", async (req, res) => {
const code = req.query.code as string;
const authResp = await sc.authenticateWithCode(code, redirectUri);
res.cookie("access_token", authResp.accessToken);
res.json(authResp.accessToken);
});

app.listen(3000, () => {
console.log("Server is running on port 3000");
});
```

## 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.

- [Express.js](https://github.com/scalekit-inc/scalekit-express-example.git)
- [Next.js](https://github.com/scalekit-inc/scalekit-nextjs-example.git)

## API Reference
See the [Scalekit API docs](https://docs.scalekit.com) for more information about the API and authentication.

Refer to our [API reference docs](https://docs.scalekit.com/apis) for detailed information about all our API endpoints and their usage.

## More Information

- Quickstart Guide to implement Single Sign-on in your application: [SSO Quickstart Guide](https://docs.scalekit.com)
- Understand Single Sign-on basics: [SSO Basics](https://docs.scalekit.com/best-practices/single-sign-on)

## License

This project is licensed under the **MIT license**.
See the [LICENSE](LICENSE) file for more information.
See the [LICENSE](LICENSE) file for more information.
9 changes: 5 additions & 4 deletions lib/connect.js

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

2 changes: 1 addition & 1 deletion lib/connect.js.map

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

20 changes: 17 additions & 3 deletions lib/connection.d.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import GrpcConnect from './connect';
import CoreClient from './core';
import { GetConnectionResponse, ListConnectionsResponse } from './pkg/grpc/scalekit/v1/connections/connections_pb';
import { GetConnectionResponse, ToggleConnectionResponse, ListConnectionsResponse } from './pkg/grpc/scalekit/v1/connections/connections_pb';
export default class ConnectionClient {
private readonly grpcConncet;
private readonly coreClient;
private client;
constructor(grpcConncet: GrpcConnect, coreClient: CoreClient);
/**
* Get a connection by id and organization id
* @param id The connection id
* @param organizationId The organization id
* @param id The connection id
* @returns {Promise<GetConnectionResponse>} The connection
*/
getConnection(id: string, organizationId: string): Promise<GetConnectionResponse>;
getConnection(organizationId: string, id: string): Promise<GetConnectionResponse>;
/**
* List connections by domain
* @param domain The domain
Expand All @@ -25,4 +25,18 @@ export default class ConnectionClient {
* @returns {Promise<ListConnectionsResponse>} The list of connections
*/
listConnections(organizationId: string): Promise<ListConnectionsResponse>;
/**
* Enable a connection by id and organization id
* @param organizationId The organization id
* @param id The connection id
* @returns {Promise<ToggleConnectionResponse>} The connection enable response
*/
enableConnection(organizationId: string, id: string): Promise<ToggleConnectionResponse>;
/**
* Disable a connection by id and organization id
* @param organizationId The organization id
* @param id The connection id
* @returns {Promise<ToggleConnectionResponse>} The connection enable response
*/
disableConnection(organizationId: string, id: string): Promise<ToggleConnectionResponse>;
}
38 changes: 36 additions & 2 deletions lib/connection.js

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

2 changes: 1 addition & 1 deletion lib/connection.js.map

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

2 changes: 1 addition & 1 deletion lib/constants/user.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { IdTokenClaim, User } from '../types/user';
import type { IdTokenClaim, User } from '../types/auth';
export declare const IdTokenClaimToUserMap: {
[k in keyof IdTokenClaim]: keyof User;
};
14 changes: 9 additions & 5 deletions lib/core.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Axios, AxiosResponse } from "axios";
import { JWK } from 'jose';
import { TokenResponse } from './types/auth';
export declare const headers: {
"user-agent": string;
"x-sdk-version": string;
"x-api-version": string;
authorization: string;
};
export default class CoreClient {
readonly envUrl: string;
readonly clientId: string;
Expand All @@ -15,12 +22,9 @@ export default class CoreClient {
/**
* Authenticate with the code
* @param {string} data Data to authenticate
* @returns {Promise<AxiosResponse<{ access_token: string, id_token: string }>>} Returns access token and id token
* @returns {Promise<AxiosResponse<TokenResponse>>} Returns access token and id token
*/
authenticate(data: string): Promise<AxiosResponse<{
access_token: string;
id_token: string;
}, any>>;
authenticate(data: string): Promise<AxiosResponse<TokenResponse, any>>;
/**
* Get the JWKS from the server and store it in the client instance
* @returns {Promise<void>} Returns nothing
Expand Down
Loading

0 comments on commit 53ac4c3

Please sign in to comment.