Skip to content

Commit

Permalink
New packages for Auth SDK (#797)
Browse files Browse the repository at this point in the history
* Cleans up how refresh tokens are received to avoid fetching multiple times

* lint

* removes unnecessary usecallbacks

* Creates new Auth SDKs

* Removes tests as we dont have them yet

* changeset

* Adds log to invalid jwt verification

* lint
  • Loading branch information
AlbertoElias authored Oct 8, 2024
1 parent 7bcc4c4 commit 127ccc6
Show file tree
Hide file tree
Showing 41 changed files with 374 additions and 56 deletions.
5 changes: 5 additions & 0 deletions .changeset/empty-bees-occur.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@crossmint/server-sdk-auth": minor
---

Initial release
5 changes: 5 additions & 0 deletions .changeset/good-months-hear.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@crossmint/client-sdk-auth": minor
---

Initial release
5 changes: 5 additions & 0 deletions .changeset/serious-camels-hang.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@crossmint/client-sdk-react-ui": patch
---

Moves from auth core to new auth sdk
5 changes: 5 additions & 0 deletions .changeset/strange-knives-relax.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@crossmint/client-sdk-auth-core": patch
---

Deprecated in favour of @crossmint/common-sdk-auth
5 changes: 5 additions & 0 deletions .changeset/stupid-socks-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@crossmint/common-sdk-auth": minor
---

Initial release
27 changes: 4 additions & 23 deletions apps/payments/create-react-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,37 +5,18 @@
"repository": "https://github.com/Crossmint/crossmint-sdk",
"license": "Apache-2.0",
"author": "Paella Labs Inc",
"files": [
"public",
"src",
".env",
"LICENSE",
"package.json",
"README.md",
"tsconfig.json",
"yarn.lock"
],
"files": ["public", "src", ".env", "LICENSE", "package.json", "README.md", "tsconfig.json", "yarn.lock"],
"scripts": {
"build": "react-scripts build",
"eject": "react-scripts eject",
"start": "react-scripts start"
},
"browserslist": {
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
"production": [">0.2%", "not dead", "not op_mini all"],
"development": ["last 1 chrome version", "last 1 firefox version", "last 1 safari version"]
},
"eslintConfig": {
"extends": [
"react-app"
]
"extends": ["react-app"]
},
"dependencies": {
"@crossmint/client-sdk-react-ui": "workspace:*",
Expand Down
8 changes: 8 additions & 0 deletions crossmint-sdk.code-workspace
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,18 @@
"name": "🧬 @crossmint/common-sdk-base",
"path": "packages/common/base"
},
{
"name": "🧬 @crossmint/common-sdk-auth",
"path": "packages/common/auth"
},
{
"name": "🧬 @crossmint/client-sdk-base",
"path": "packages/client/base"
},
{
"name": "⚛️ @crossmint/client-sdk-auth",
"path": "packages/client/auth"
},
{
"name": "⚛️ @crossmint/client-sdk-react-ui",
"path": "packages/client/ui/react-ui"
Expand Down
3 changes: 3 additions & 0 deletions packages/client/auth-core/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# @crossmint/client-sdk-auth-core

DEPRECATED: This package is deprecated and has been replaced by @crossmint/common-sdk-auth, @crossmint/client-sdk-auth and @crossmint/server-sdk-auth.
6 changes: 1 addition & 5 deletions packages/client/auth-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,7 @@
"require": "./dist/client/index.cjs"
}
},
"files": [
"dist",
"src",
"LICENSE"
],
"files": ["dist", "src", "LICENSE"],
"scripts": {
"build": "tsup server/index.ts client/index.ts --clean --format esm,cjs --outDir ./dist --minify --dts --sourcemap",
"dev": "tsup server/index.ts client/index.ts --clean --format esm,cjs --outDir ./dist --dts --sourcemap --watch",
Expand Down
2 changes: 2 additions & 0 deletions packages/client/auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# @crossmint/client-sdk-auth

25 changes: 25 additions & 0 deletions packages/client/auth/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "@crossmint/client-sdk-auth",
"version": "0.1.0",
"repository": "https://github.com/Crossmint/crossmint-sdk",
"license": "Apache-2.0",
"author": "Paella Labs Inc",
"sideEffects": false,
"type": "module",
"exports": {
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": ["dist", "src", "LICENSE"],
"scripts": {
"build": "tsup src/index.ts --clean --format esm,cjs --outDir ./dist --minify --dts --sourcemap",
"dev": "tsup src/index.ts --clean --format esm,cjs --outDir ./dist --dts --sourcemap --watch"
},
"dependencies": {
"@crossmint/common-sdk-auth": "workspace:*",
"jwt-decode": "4.0.0"
}
}
2 changes: 2 additions & 0 deletions packages/client/auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { CrossmintAuthService } from "@crossmint/common-sdk-auth";
export * from "./utils";
1 change: 1 addition & 0 deletions packages/client/auth/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./jwt";
6 changes: 6 additions & 0 deletions packages/client/auth/src/utils/jwt.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { jwtDecode } from "jwt-decode";

export function getJWTExpiration(token: string) {
const decoded = jwtDecode(token);
return decoded.exp;
}
11 changes: 11 additions & 0 deletions packages/client/auth/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
}
},
"include": ["src/**/*"]
}
9 changes: 3 additions & 6 deletions packages/client/ui/react-ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,19 @@
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"dist",
"src",
"LICENSE"
],
"files": ["dist", "src", "LICENSE"],
"scripts": {
"build": "tsup src/index.ts --clean --external react,react-dom --format esm,cjs --outDir ./dist --minify --dts --sourcemap",
"dev": "tsup src/index.ts --clean --external react,react-dom --format esm,cjs --outDir ./dist --dts --sourcemap --watch",
"test": "vitest run"
},
"dependencies": {
"@crossmint/client-sdk-auth-core": "workspace:*",
"@crossmint/client-sdk-auth": "workspace:*",
"@crossmint/client-sdk-base": "workspace:*",
"@crossmint/client-sdk-smart-wallet": "workspace:*",
"@crossmint/client-sdk-window": "workspace:*",
"@crossmint/common-sdk-base": "workspace:*",
"@crossmint/common-sdk-auth": "workspace:*",
"@ethersproject/transactions": "5.7.0",
"@headlessui/react": "2.1.5",
"@solana/web3.js": "1.95.1",
Expand Down
4 changes: 2 additions & 2 deletions packages/client/ui/react-ui/src/hooks/useRefreshToken.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { act, renderHook } from "@testing-library/react";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";

import { type CrossmintAuthService, getJWTExpiration } from "@crossmint/client-sdk-auth-core/client";
import { type CrossmintAuthService, getJWTExpiration } from "@crossmint/client-sdk-auth";
import { queueTask } from "@crossmint/client-sdk-base";

import * as authCookies from "../utils/authCookies";
import { type AuthMaterial, useRefreshToken } from "./useRefreshToken";

vi.mock("@crossmint/client-sdk-auth-core", () => ({
vi.mock("@crossmint/client-sdk-auth", () => ({
CrossmintAuthService: vi.fn(),
getJWTExpiration: vi.fn(),
}));
Expand Down
14 changes: 3 additions & 11 deletions packages/client/ui/react-ui/src/hooks/useRefreshToken.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
import { useCallback, useEffect, useRef } from "react";

import type { CrossmintAuthService, SDKExternalUser } from "@crossmint/client-sdk-auth-core/client";
import { getJWTExpiration } from "@crossmint/client-sdk-auth-core/client";
import type { AuthMaterial } from "@crossmint/common-sdk-auth";
import type { CrossmintAuthService } from "@crossmint/client-sdk-auth";
import { getJWTExpiration } from "@crossmint/client-sdk-auth";
import { queueTask, type CancellableTask } from "@crossmint/client-sdk-base";

import { REFRESH_TOKEN_PREFIX, getCookie } from "../utils/authCookies";

// 2 minutes before jwt expiration
const TIME_BEFORE_EXPIRING_JWT_IN_SECONDS = 120;

export type AuthMaterial = {
jwtToken: string;
refreshToken: {
secret: string;
expiresAt: string;
};
user: SDKExternalUser;
};

type UseAuthTokenRefreshProps = {
crossmintAuthService: CrossmintAuthService;
setAuthMaterial: (authMaterial: AuthMaterial) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { type ReactNode, act } from "react";
import { beforeEach, describe, expect, vi } from "vitest";
import { mock } from "vitest-mock-extended";

import { CrossmintAuthService, getJWTExpiration } from "@crossmint/client-sdk-auth-core/client";
import { CrossmintAuthService, getJWTExpiration } from "@crossmint/client-sdk-auth";
import { type EVMSmartWallet, SmartWalletSDK } from "@crossmint/client-sdk-smart-wallet";
import { createCrossmint } from "@crossmint/common-sdk-base";

Expand Down Expand Up @@ -32,8 +32,8 @@ vi.mock("@crossmint/common-sdk-base", async () => {
};
});

vi.mock("@crossmint/client-sdk-auth-core/client", async () => {
const actual = await vi.importActual("@crossmint/client-sdk-auth-core/client");
vi.mock("@crossmint/client-sdk-auth", async () => {
const actual = await vi.importActual("@crossmint/client-sdk-auth");
return {
...actual,
getJWTExpiration: vi.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ import { REFRESH_TOKEN_PREFIX, SESSION_PREFIX, deleteCookie, getCookie, setCooki
import { type ReactNode, createContext, useEffect, useState } from "react";
import { createPortal } from "react-dom";

import { CrossmintAuthService, type SDKExternalUser } from "@crossmint/client-sdk-auth-core/client";
import { CrossmintAuthService } from "@crossmint/client-sdk-auth";
import type { EVMSmartWalletChain } from "@crossmint/client-sdk-smart-wallet";
import { type UIConfig, validateApiKeyAndGetCrossmintBaseUrl } from "@crossmint/common-sdk-base";

import AuthModal from "../components/auth/AuthModal";
import { type AuthMaterial, useCrossmint, useRefreshToken, useWallet } from "../hooks";
import { useCrossmint, useRefreshToken, useWallet } from "../hooks";
import { CrossmintWalletProvider } from "./CrossmintWalletProvider";
import type { AuthMaterial, SDKExternalUser } from "@crossmint/common-sdk-auth";

export type CrossmintAuthWalletConfig = {
defaultChain: EVMSmartWalletChain;
Expand Down
2 changes: 2 additions & 0 deletions packages/common/auth/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# @crossmint/common-sdk-auth

24 changes: 24 additions & 0 deletions packages/common/auth/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"name": "@crossmint/common-sdk-auth",
"version": "0.1.0",
"repository": "https://github.com/Crossmint/crossmint-sdk",
"license": "Apache-2.0",
"author": "Paella Labs Inc",
"sideEffects": false,
"type": "module",
"main": "./dist/index.cjs",
"module": "./dist/index.js",
"types": "./dist/index.d.ts",
"exports": {
"import": "./dist/index.js",
"require": "./dist/index.cjs"
},
"files": ["dist", "src", "LICENSE"],
"scripts": {
"build": "tsup src/index.ts --clean --format esm,cjs --outDir ./dist --minify --dts --sourcemap",
"dev": "tsup src/index.ts --clean --format esm,cjs --outDir ./dist --dts --sourcemap --watch"
},
"dependencies": {
"@crossmint/client-sdk-base": "workspace:*"
}
}
3 changes: 3 additions & 0 deletions packages/common/auth/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export * from "./services";
export * from "./utils";
export * from "./types";
40 changes: 40 additions & 0 deletions packages/common/auth/src/services/CrossmintAuthService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { APIErrorService, BaseCrossmintService } from "@crossmint/client-sdk-base";

import { authLogger } from "./logger";
import type { AuthMaterial } from "@/types";

export class CrossmintAuthService extends BaseCrossmintService {
protected apiErrorService = new APIErrorService<never>({});
protected logger = authLogger;

public getJWKSUri() {
return `${this.crossmintBaseUrl}/.well-known/jwks.json`;
}

async refreshAuthMaterial(refreshToken: string): Promise<AuthMaterial> {
const result = await this.fetchCrossmintAPI(
"2024-09-26/session/sdk/auth/refresh",
{ method: "POST", body: JSON.stringify({ refresh: refreshToken }) },
"Error fetching new refresh and access tokans."
);

return {
jwtToken: result.jwt,
refreshToken: result.refresh,
user: result.user,
};
}

async getUserFromServer(externalUserId: string) {
const result = await this.fetchCrossmintAPI(
`sdk/auth/user/${externalUserId}`,
{ method: "GET" },
"Error fetching user."
);

return result.user;
}
async getUserFromClient(jwt: string) {
return await this.fetchCrossmintAPI("sdk/auth/user", { method: "GET" }, "Error fetching user.", jwt);
}
}
2 changes: 2 additions & 0 deletions packages/common/auth/src/services/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./CrossmintAuthService";
export * from "./logger";
5 changes: 5 additions & 0 deletions packages/common/auth/src/services/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { SDKLogger } from "@crossmint/client-sdk-base";

import { AUTH_SERVICE } from "../utils/constants";

export const authLogger = new SDKLogger(AUTH_SERVICE);
10 changes: 10 additions & 0 deletions packages/common/auth/src/types/authmaterial.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { SDKExternalUser } from "./user";

export type AuthMaterial = {
jwtToken: string;
refreshToken: {
secret: string;
expiresAt: string;
};
user: SDKExternalUser;
};
2 changes: 2 additions & 0 deletions packages/common/auth/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from "./user";
export * from "./authmaterial";
16 changes: 16 additions & 0 deletions packages/common/auth/src/types/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export type FarcasterMetadata = {
fid: string;
username: string;
bio: string;
displayName: string;
pfpUrl: string;
custody: string;
verifications: string[];
};

export type SDKExternalUser = {
id: string;
email?: string;
phoneNumber?: string;
farcaster?: FarcasterMetadata;
};
1 change: 1 addition & 0 deletions packages/common/auth/src/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const AUTH_SERVICE = "AUTH_SDK";
1 change: 1 addition & 0 deletions packages/common/auth/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./constants";
11 changes: 11 additions & 0 deletions packages/common/auth/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "../../../tsconfig.base.json",
"compilerOptions": {
"rootDir": "src",
"baseUrl": ".",
"paths": {
"@/*": ["src/*"]
}
},
"include": ["src/**/*"]
}
Loading

0 comments on commit 127ccc6

Please sign in to comment.