|
| 1 | +import config from '../config'; |
| 2 | +import { PublicApiAxiosParamCreator, AdminApiAxiosParamCreator } from '@oryd/kratos-client' |
| 3 | + |
| 4 | +const capitalize = (str) => `${str[0].toUpperCase()}${str.slice(1)}` |
| 5 | +/** |
| 6 | + * Kratos redirects and sets cookie upon client lands on URL and redirects back to |
| 7 | + * frontend with request_id |
| 8 | + * */ |
| 9 | +const authPublicURL = `${config.backendURL}/.ory/kratos/public`; |
| 10 | +const authAdminURL = `${config.backendURL}/.ory/kratos`; |
| 11 | + |
| 12 | +/** |
| 13 | + * publicApi / adminApi are initialized Kratos SDK client with public / admin endpoints, |
| 14 | + * we use the sdk map out endpoints for http client to fetch |
| 15 | + */ |
| 16 | +const publicApi = new PublicApiAxiosParamCreator({basePath : authPublicURL }); |
| 17 | +const adminApi = new AdminApiAxiosParamCreator({basePath : authAdminURL }); |
| 18 | + |
| 19 | +/** |
| 20 | + * getBrowserFlowParams / getRequestFlowData |
| 21 | + * maps and calls flows[Login/Registration/Recovery/Settings] to kratos sdk fn names |
| 22 | + */ |
| 23 | +const getBrowserFlowParams = async (flow) => publicApi[`initializeSelfService${flow}ViaBrowserFlow`](); |
| 24 | +const getRequestFlowData = async (flow, id) => publicApi[`getSelfService${flow}Flow`](id); |
| 25 | + |
| 26 | +/** |
| 27 | + * generate<Logout/FormRequest/RequestData/Session>URL uses SDK to generate endpoint for http client |
| 28 | + */ |
| 29 | +const generateLogoutUrl = async () => { |
| 30 | + const { url } = await publicApi.initializeSelfServiceBrowserLogoutFlow(); |
| 31 | + return authPublicURL + url; |
| 32 | +} |
| 33 | + |
| 34 | +const generateFormRequestUrl = async (type) => { |
| 35 | + let { url } = await getBrowserFlowParams(capitalize(type)); |
| 36 | + // Workaround for bug in SDK specs: https://github.com/ory/sdk/issues/43 |
| 37 | + if (type == "settings") { |
| 38 | + url = url.replace(/\/flows$/, ''); |
| 39 | + } |
| 40 | + return authPublicURL + url; |
| 41 | +} |
| 42 | + |
| 43 | +const generateRequestDataUrl = async (type, flowId) => { |
| 44 | + const { url } = await getRequestFlowData(capitalize(type), flowId); |
| 45 | + return authAdminURL + url; |
| 46 | +}; |
| 47 | + |
| 48 | +const generateSessionUrl = async (type, flowId) => { |
| 49 | + const { url } = await publicApi.whoami(); |
| 50 | + return authPublicURL + url; |
| 51 | +}; |
| 52 | + |
| 53 | +/** |
| 54 | + * fetchRequestData Assumes proxy(oathkeeper) to forward this request to admin-endpoint |
| 55 | + * and returns the form fields / destination |
| 56 | + * @param {*} type the form type (login/registration/recovery/settings) |
| 57 | + * @param {*} flowId the generated ID from the kratos redirect |
| 58 | + */ |
| 59 | +const fetchRequestData = async (type = "login", flowId) => { |
| 60 | + const uri = await generateRequestDataUrl(type, flowId); |
| 61 | + const options = { method: 'GET', headers: { Accept: 'application/json' } }; |
| 62 | + const response = await fetch(uri, options); |
| 63 | + if (response.status >= 400 && response.status < 500) { |
| 64 | + return false; |
| 65 | + } |
| 66 | + return response.json(); |
| 67 | + }; |
| 68 | + |
| 69 | +/** |
| 70 | + * fetchAuthState authenticates with the public endpoint of kratos, |
| 71 | + * given a valid session's cookie, kratos will respond with session information |
| 72 | + * that includes whether session is active, checking it then formatting for authContext |
| 73 | + */ |
| 74 | +const fetchAuthState = async() => { |
| 75 | + const uri = await generateSessionUrl(); |
| 76 | + const options = {credentials: "include"}; |
| 77 | + |
| 78 | + const response = await fetch(uri, options); |
| 79 | + const authResult = await response.json(); |
| 80 | + /** Kratos session payload example: |
| 81 | + * { |
| 82 | + "id": "872ea955-59c0-4417-add1-a9f824bb2f8d", |
| 83 | + "active": true, |
| 84 | + "expires_at": "2020-11-06T21:24:10.566549283Z", |
| 85 | + "authenticated_at": "2020-11-05T21:24:10.566549283Z", |
| 86 | + "issued_at": "2020-11-05T21:24:10.56655868Z", |
| 87 | + "identity": { |
| 88 | + "id": "db0eba56-f7e6-4a7c-8b8e-5ed3c802139c", |
| 89 | + "schema_id": "default", |
| 90 | + "schema_url": "https://<your-application>/.ory/kratos/public/schemas/default", |
| 91 | + "traits": { |
| 92 | + "email": "user@example.com" |
| 93 | + } |
| 94 | + } |
| 95 | +} */ |
| 96 | + const isLoggedIn = !!authResult.active |
| 97 | + return { |
| 98 | + isAuthenticated: isLoggedIn, |
| 99 | + user: authResult.identity?.id, |
| 100 | + email: authResult.identity?.traits?.email |
| 101 | + }; |
| 102 | +}; |
| 103 | + |
| 104 | +export { fetchRequestData, fetchAuthState, generateFormRequestUrl, generateLogoutUrl } |
0 commit comments