-
Notifications
You must be signed in to change notification settings - Fork 18
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
Showing
9 changed files
with
248 additions
and
160 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ name: Build | |
on: | ||
push: | ||
branches-ignore: | ||
- 5.x | ||
- main | ||
|
||
jobs: | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* Copyright 2021 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
import { cleanupHeaderValue } from '@adobe/helix-shared-utils'; | ||
import setCustomResponseHeaders from './steps/set-custom-response-headers.js'; | ||
import { PipelineResponse } from './PipelineResponse.js'; | ||
import { validateAuthState, getAuthInfo } from './utils/auth.js'; | ||
|
||
/** | ||
* Runs the auth pipeline that handles the token exchange. this is separated from the main pipeline | ||
* since it doesn't need the configuration. | ||
* | ||
* @param {PipelineState} state | ||
* @param {PipelineRequest} req | ||
* @returns {PipelineResponse} | ||
*/ | ||
export async function authPipe(state, req) { | ||
const { log } = state; | ||
|
||
/** @type PipelineResponse */ | ||
const res = new PipelineResponse('', { | ||
headers: { | ||
'content-type': 'text/html; charset=utf-8', | ||
}, | ||
}); | ||
|
||
try { | ||
await validateAuthState(state, req); | ||
const authInfo = await getAuthInfo(state, req); | ||
await authInfo.exchangeToken(state, req, res); | ||
/* c8 ignore next */ | ||
const level = res.status >= 500 ? 'error' : 'info'; | ||
log[level](`pipeline status: ${res.status} ${res.error}`); | ||
res.headers.set('x-error', cleanupHeaderValue(res.error)); | ||
if (res.status < 500) { | ||
await setCustomResponseHeaders(state, req, res); | ||
} | ||
return res; | ||
} catch (e) { | ||
return new PipelineResponse('', { | ||
status: 401, | ||
headers: { | ||
'cache-control': 'no-store, private, must-revalidate', | ||
'content-type': 'text/html; charset=utf-8', | ||
'x-error': e.message, | ||
}, | ||
}); | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
/* | ||
* Copyright 2021 Adobe. All rights reserved. | ||
* This file is licensed to you under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. You may obtain a copy | ||
* of the License at http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software distributed under | ||
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS | ||
* OF ANY KIND, either express or implied. See the License for the specific language | ||
* governing permissions and limitations under the License. | ||
*/ | ||
|
||
/* eslint-env mocha */ | ||
import assert from 'assert'; | ||
import { exportJWK, generateKeyPair, SignJWT } from 'jose'; | ||
import { FileS3Loader } from './FileS3Loader.js'; | ||
import { | ||
authPipe, PipelineRequest, PipelineState, | ||
} from '../src/index.js'; | ||
|
||
const DEFAULT_CONFIG = { | ||
contentBusId: 'foo-id', | ||
owner: 'adobe', | ||
repo: 'helix-pages', | ||
ref: 'main', | ||
}; | ||
|
||
const DEFAULT_STATE = (config = DEFAULT_CONFIG, opts = {}) => (new PipelineState({ | ||
config, | ||
site: 'site', | ||
org: 'org', | ||
ref: 'ref', | ||
partition: 'preview', | ||
s3Loader: new FileS3Loader(), | ||
...opts, | ||
})); | ||
|
||
describe('Auth Pipe Test', () => { | ||
it('handles /.auth route', async () => { | ||
const keyPair = await generateKeyPair('RS256'); | ||
const { privateKey, publicKey } = keyPair; | ||
const env = { | ||
HLX_ADMIN_IDP_PUBLIC_KEY: JSON.stringify({ | ||
...await exportJWK(publicKey), | ||
kid: 'dummy-kid', | ||
}), | ||
HLX_ADMIN_IDP_PRIVATE_KEY: JSON.stringify(await exportJWK(privateKey)), | ||
HLX_SITE_APP_AZURE_CLIENT_ID: 'dummy-clientid', | ||
}; | ||
|
||
const tokenState = await new SignJWT({ | ||
url: 'https://www.hlx.live/en', | ||
}) | ||
.setProtectedHeader({ alg: 'RS256', kid: 'dummy-kid' }) | ||
.setIssuer('urn:example:issuer') | ||
.setAudience('dummy-clientid') | ||
.sign(privateKey); | ||
|
||
const state = DEFAULT_STATE(DEFAULT_CONFIG, { | ||
env, | ||
path: '/.auth', | ||
}); | ||
const req = new PipelineRequest('https://localhost/.auth', { | ||
headers: new Map(Object.entries({ | ||
'x-hlx-auth-state': tokenState, | ||
'x-hlx-auth-code': '1234-code', | ||
})), | ||
}); | ||
const resp = await authPipe(state, req); | ||
assert.strictEqual(resp.status, 302); | ||
assert.strictEqual(resp.headers.get('location'), `https://www.hlx.live/.auth?state=${tokenState}&code=1234-code`); | ||
}); | ||
|
||
it('handles error in the /.auth route', async () => { | ||
const state = DEFAULT_STATE(DEFAULT_CONFIG, { | ||
path: '/.auth', | ||
}); | ||
const req = new PipelineRequest('https://localhost/.auth', { | ||
headers: new Map(Object.entries({ | ||
'x-hlx-auth-state': 'invalid', | ||
'x-hlx-auth-code': '1234-code', | ||
})), | ||
}); | ||
const resp = await authPipe(state, req); | ||
assert.strictEqual(resp.status, 401); | ||
}); | ||
}); |
Oops, something went wrong.