Skip to content

Commit

Permalink
Don't sync when end-user license expired.
Browse files Browse the repository at this point in the history
+ allow token endpoint to send error instead of tokens
  • Loading branch information
dfahlander committed Sep 27, 2023
1 parent 4b7659e commit a500d07
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
16 changes: 10 additions & 6 deletions addons/dexie-cloud/src/authentication/authenticate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export type FetchTokenCallback = (tokenParams: {

export async function loadAccessToken(
db: DexieCloudDB
): Promise<string | undefined> {
): Promise<UserLogin | null> {
const currentUser = await db.getCurrentUser();
const {
accessToken,
Expand All @@ -32,10 +32,10 @@ export async function loadAccessToken(
refreshTokenExpiration,
claims,
} = currentUser;
if (!accessToken) return;
if (!accessToken) return null;
const expTime = accessTokenExpiration?.getTime() ?? Infinity;
if (expTime > Date.now()) {
return accessToken;
return currentUser;
}
if (!refreshToken) {
throw new Error(`Refresh token missing`);
Expand All @@ -51,8 +51,9 @@ export async function loadAccessToken(
await db.table('$logins').update(claims.sub, {
accessToken: refreshedLogin.accessToken,
accessTokenExpiration: refreshedLogin.accessTokenExpiration,
license: refreshedLogin.license
});
return refreshedLogin.accessToken;
return refreshedLogin;
}

export async function authenticate(
Expand Down Expand Up @@ -116,15 +117,18 @@ export async function refreshAccessToken(
});
if (res.status !== 200)
throw new Error(`RefreshToken: Status ${res.status} from ${url}/token`);
const response: TokenFinalResponse = await res.json();
const response: TokenFinalResponse | TokenErrorResponse = await res.json();
if (response.type === 'error') {
throw new TokenErrorResponseError(response);
}
login.accessToken = response.accessToken;
login.accessTokenExpiration = response.accessTokenExpiration
? new Date(response.accessTokenExpiration)
: undefined;
login.claims = response.claims;
login.license = {
type: response.userType,
status: response.claims.license || 'ok'
status: response.claims.license || 'ok',
}
if (response.evalDaysLeft != null) {
login.license.evalDaysLeft = response.evalDaysLeft;
Expand Down
13 changes: 12 additions & 1 deletion addons/dexie-cloud/src/sync/syncWithServer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,18 @@ export async function syncWithServer(
Accept: 'application/json, application/x-bison, application/x-bison-stream',
'Content-Type': 'application/tson'
};
const accessToken = await loadAccessToken(db);
const updatedUser = await loadAccessToken(db);
if (updatedUser) {
if (updatedUser.license && updatedUser.license.status === 'expired') {
throw new Error(
`License has expired`
);
}
if (updatedUser.license && updatedUser.license.status === 'deactivated') {
throw new Error(`License deactivated`);
}
}
const accessToken = updatedUser?.accessToken;
if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`;
}
Expand Down

0 comments on commit a500d07

Please sign in to comment.