Authentication
Authenticating with the LearnCloud Storage API
To interact with the LearnCloud Storage API, you can choose one of two ways to authenticate:
Using the LearnCloud Storage Plugin (
@learncard/learn-cloud-plugin) which handles authentication for you. (Preferred option)Directly through the API endpoints using challenge-based DID Authentication. (most complex)
1. Using LearnCloud Storage Plugin
To authenticate using the LearnCloud Storage Plugin (@learncard/learn-cloud-plugin), first install the package:
pnpm install @learncard/learn-cloud-pluginThen, either instantiate a LearnCloud STorage enabled LearnCard, or add the Storage Plugin to an existing LearnCard:
import { initLearnCard } from '@learncard/init';
import didkit from '@learncard/didkit-plugin/dist/didkit/didkit_wasm_bg.wasm?url';
const storageLearnCard = await initLearnCard({
seed,
network: true,
didkit,
});import { initLearnCard } from '@learncard/init';
import { getLearnCloudPlugin } from '@learncard/learn-cloud-plugin';
import didkit from '@learncard/didkit-plugin/dist/didkit/didkit_wasm_bg.wasm?url';
const storageAPI = 'https://cloud.learncard.com/trpc';
const learnCard = await initLearnCard({
seed,
didkit,
});
const storageLearnCard = await learnCard.addPlugin(
await getLearnCloudPlugin(learnCard, storageAPI)
);When using the LearnCloud Storage Plugin, challenge-based DID Authentication is handled for you, so no further steps are necessary.
2. Using Challenge-based DID Authentication
Storage API uses DID-based authentication with a challenge-response mechanism and scope-based authorization.
Simple High-Level Auth Flow:
Granular Auth Flow:
If you choose to use the API endpoints directly, you'll need to manage challenge-based DID Authentication for each request. Here's a simplified TypeScript example to help you implement this authentication method:
In this example, we first define a getClient function that takes a url and a didAuthFunction. The didAuthFunction should be an asynchronous function that returns a signed challenge as a string.
The getChallenges function fetches a list of challenges from the API. The getAuthHeaders function generates an Authorization header using the didAuthFunction and a challenge. This header can then be used in your API calls.
Authorization
Route Middleware
The system uses several middleware layers for authentication and authorization:
openRoute: Base middleware that allows public accessdidRoute: Requires a valid DID in the requestdidAndChallengeRoute: Requires a valid DID and challengeprofileRoute: Requires a valid DID, challenge, and existing profile
Last updated
Was this helpful?