-
-
Notifications
You must be signed in to change notification settings - Fork 39
feat(endpoint-auth): support profile scope and add userinfo endpoint #938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,11 @@ | ||
| <!DOCTYPE html> | ||
| <html lang="en"> | ||
| <head> | ||
| <title>Jane Example</title> | ||
| </head> | ||
| <body class="h-card"> | ||
| <img class="u-photo" src="/photo.jpg" alt=""> | ||
| <h1><a class="p-name u-url" href="https://website.example">Jane Example</a></h1> | ||
| <p class="p-note">Writes about cheese sandwiches.</p> | ||
| </body> | ||
| </html> |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,44 @@ | ||
| import { IndiekitError } from "@indiekit/error"; | ||
|
|
||
| import { getProfileInformation } from "../profile.js"; | ||
| import { verifyToken } from "../token.js"; | ||
|
|
||
| /** | ||
| * User information request | ||
| * | ||
| * Return profile information for the user an access token was issued to. | ||
| * @param {object} [options] - Plug-in options | ||
| * @param {object} [options.profile] - Configured profile information | ||
| * @returns {import("express").RequestHandler} Controller | ||
| * @see {@link https://indieauth.spec.indieweb.org/#user-information} | ||
| */ | ||
| export const userinfoController = | ||
| (options = {}) => | ||
| async (request, response, next) => { | ||
| try { | ||
| let accessToken; | ||
| try { | ||
| // Remove ‘Bearer ’ from authorization header | ||
| const token = request.headers.authorization?.trim().split(/\s+/, 2)[1]; | ||
| accessToken = verifyToken(token); | ||
| } catch { | ||
| throw IndiekitError.unauthorized( | ||
| response.locals.__("UnauthorizedError.invalidToken"), | ||
| ); | ||
| } | ||
|
|
||
| const { me, scope } = accessToken; | ||
| if (!scope?.split(" ").includes("profile")) { | ||
| throw IndiekitError.insufficientScope( | ||
| response.locals.__("ForbiddenError.insufficientScope"), | ||
| { scope: "profile" }, | ||
| ); | ||
| } | ||
|
|
||
| const profile = await getProfileInformation(me, options.profile); | ||
|
|
||
| response.json(profile || {}); | ||
| } catch (error) { | ||
| next(error); | ||
| } | ||
| }; |
This file contains hidden or 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,93 @@ | ||
| import { mf2 } from "microformats-parser"; | ||
|
|
||
| import { isFetchableOrigin } from "./client.js"; | ||
|
|
||
| const FETCH_TIMEOUT = 5000; | ||
|
|
||
| /** | ||
| * Get first value of a microformats property, as a string | ||
| * @param {Array} [values] - Property values | ||
| * @returns {string|undefined} Value | ||
| */ | ||
| const getValue = (values) => { | ||
| const value = values?.[0]; | ||
| return typeof value === "object" ? value.value : value; | ||
| }; | ||
|
|
||
| /** | ||
| * Discover profile information from the representative h-card on a user’s | ||
| * site, preferring an h-card whose URL is the profile URL | ||
| * @param {string} me - Profile URL | ||
| * @returns {Promise<object>} Discovered profile information | ||
| * @see {@link https://microformats.org/wiki/representative-h-card-parsing} | ||
| */ | ||
| const discoverProfile = async (me) => { | ||
| if (!URL.canParse(me) || !isFetchableOrigin(new URL(me))) { | ||
| return {}; | ||
| } | ||
|
|
||
| let body; | ||
| try { | ||
| const response = await fetch(me, { | ||
| headers: { accept: "text/html" }, | ||
| signal: AbortSignal.timeout(FETCH_TIMEOUT), | ||
| }); | ||
|
|
||
| if (!response.ok) { | ||
| return {}; | ||
| } | ||
|
|
||
| body = await response.text(); | ||
| } catch { | ||
| return {}; | ||
| } | ||
|
|
||
| let items; | ||
| try { | ||
| ({ items } = mf2(body, { baseUrl: me })); | ||
| } catch { | ||
| return {}; | ||
| } | ||
|
|
||
| const cards = items.filter((item) => item.type?.includes("h-card")); | ||
| const canonical = me.replace(/\/$/, ""); | ||
| const card = | ||
| cards.find((item) => | ||
| item.properties.url?.some((url) => getValue([url]) === canonical), | ||
| ) || cards[0]; | ||
|
|
||
| if (!card) { | ||
| return {}; | ||
| } | ||
|
|
||
| return { | ||
| name: getValue(card.properties.name), | ||
| url: getValue(card.properties.url) || me, | ||
| photo: getValue(card.properties.photo), | ||
| }; | ||
| }; | ||
|
|
||
| /** | ||
| * Get profile information for a user, using configured values first and | ||
| * filling in anything missing from the h-card on their site | ||
| * @param {string} me - Profile URL | ||
| * @param {object} [configured] - Configured profile information | ||
| * @param {string} [configured.name] - Name | ||
| * @param {string} [configured.url] - URL | ||
| * @param {string} [configured.photo] - Photo URL | ||
| * @returns {Promise<object|undefined>} Profile information, if any | ||
| * @see {@link https://indieauth.spec.indieweb.org/#profile-information} | ||
| */ | ||
| export const getProfileInformation = async (me, configured = {}) => { | ||
| const discovered = await discoverProfile(me); | ||
| const profile = {}; | ||
|
|
||
| for (const key of ["name", "url", "photo"]) { | ||
| const value = configured[key] || discovered[key]; | ||
| if (value) { | ||
| profile[key] = value; | ||
| } | ||
| } | ||
|
|
||
| return Object.keys(profile).length > 0 ? profile : undefined; | ||
| }; |
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.