forked from janeirodigital/sai-js
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f7ac922
commit 5d0e84e
Showing
13 changed files
with
181 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
41 changes: 41 additions & 0 deletions
41
packages/service/src/handlers/web-push-unsubscribe-handler.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* eslint-disable class-methods-use-this */ | ||
|
||
import { from, Observable } from 'rxjs'; | ||
import { HttpHandler, HttpHandlerResponse, ForbiddenHttpError, NotFoundHttpError } from '@digita-ai/handlersjs-http'; | ||
import { getLogger } from '@digita-ai/handlersjs-logging'; | ||
import { decodeBase64, decodeWebId } from '../url-templates'; | ||
import { SessionManager } from '../session-manager'; | ||
import { AuthenticatedAuthnContext } from '../models/http-solid-context'; | ||
|
||
export class WebPushUnsubscribeHandler extends HttpHandler { | ||
private logger = getLogger(); | ||
|
||
constructor(private sessionManager: SessionManager) { | ||
super(); | ||
this.logger.info('WebPushUnsubscribeHandler::constructor'); | ||
} | ||
|
||
// TODO: validate channel info | ||
async handleAsync(context: AuthenticatedAuthnContext): Promise<HttpHandlerResponse> { | ||
const applicationId = context.authn.clientId; | ||
const webId = decodeWebId(context.request.parameters!.encodedWebId); | ||
if (webId !== context.authn.webId) { | ||
throw new ForbiddenHttpError('wrong authorization agent'); | ||
} | ||
|
||
const topic = decodeBase64(context.request.parameters.encodedTopic) | ||
const webhookChannel = await this.sessionManager.getWebhookPushTopic(webId, applicationId, topic) | ||
if (!webhookChannel) throw new NotFoundHttpError() | ||
|
||
const saiSession = await this.sessionManager.getSaiSession(webId); | ||
const response = await saiSession.rawFetch(webhookChannel.id, { method: 'DELETE' }) | ||
if (!response.ok) return { status: 502, headers: {} }; | ||
await this.sessionManager.deleteWebhookPushTopic(webId, applicationId, topic) | ||
return { status: 204, headers: {} }; | ||
} | ||
|
||
handle(context: AuthenticatedAuthnContext): Observable<HttpHandlerResponse> { | ||
this.logger.info('WebPushUnsubscribeHandler::handle'); | ||
return from(this.handleAsync(context)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.