forked from unimatrix/web
Install independent osCommerce portal with central account linking
This commit is contained in:
parent
34f3c4ae7e
commit
7ab013bd53
1 changed files with 266 additions and 6 deletions
34
website/tests/oscommerce-sso.test.mjs
Normal file
34
website/tests/oscommerce-sso.test.mjs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import test from 'node:test';
|
||||
import assert from 'node:assert/strict';
|
||||
import {createHash} from 'node:crypto';
|
||||
import {createIntegrationSSO} from '../node/integration-sso.mjs';
|
||||
|
||||
function database(user=null){
|
||||
const writes=[];
|
||||
return {writes,prepare(sql){let values=[];return {bind(...args){values=args;return this;},async first(){return sql.includes('FROM site_sessions')?user:null;},async run(){writes.push({sql,values});return {meta:{changes:1}};}};}};
|
||||
}
|
||||
test('osCommerce authorizes only its fixed callback and issues a short-lived browser state-bound code',async()=>{
|
||||
const db=database({id:'11111111-1111-4111-8111-111111111111',username:'tester',role:'user'});
|
||||
const state='s'.repeat(43),token='t'.repeat(43);
|
||||
const response=await createIntegrationSSO(db)(new Request('https://cp.dunerealms.com/sso/authorize?client=oscommerce&state='+state+'&redirect_uri=https://attacker.invalid/',{headers:{cookie:'__Host-dune_session='+token}}));
|
||||
assert.equal(response.status,302);
|
||||
const callback=new URL(response.headers.get('location'));
|
||||
assert.equal(callback.origin,'https://shop.dunerealms.com');
|
||||
assert.equal(callback.pathname,'/dune-login.php');assert.equal(callback.searchParams.get('state'),state);
|
||||
const code=callback.searchParams.get('code');assert.match(code,/^[A-Za-z0-9_-]{43}$/);
|
||||
const issue=db.writes.find(row=>row.sql.startsWith('INSERT INTO integration_sso_codes'));
|
||||
assert.equal(issue.values[0],createHash('sha256').update(code).digest('hex'));
|
||||
assert.equal(issue.values[3],'oscommerce');assert.ok(issue.values[4]>Date.now()&&issue.values[4]<=Date.now()+60000);
|
||||
assert.equal(response.headers.get('referrer-policy'),'no-referrer');
|
||||
});
|
||||
test('adding osCommerce preserves the existing Azuriom callback',async()=>{
|
||||
const response=await createIntegrationSSO(database({id:'test',username:'tester',role:'user'}))(new Request('https://cp.dunerealms.com/sso/authorize?client=shop&state='+'a'.repeat(43),{headers:{cookie:'__Host-dune_session='+'t'.repeat(43)}}));
|
||||
assert.equal(new URL(response.headers.get('location')).pathname,'/store/dune-login');
|
||||
});
|
||||
test('invalid states, unknown clients, unauthenticated redemption and expired codes are rejected',async()=>{
|
||||
const handler=createIntegrationSSO(database(),{INTEGRATION_SSO_SECRET:'x'.repeat(40)});
|
||||
for(const query of ['client=oscommerce&state=short','client=unknown&state='+'a'.repeat(43)])assert.equal((await handler(new Request('https://cp.dunerealms.com/sso/authorize?'+query))).status,400);
|
||||
const request=(auth)=>new Request('https://cp.dunerealms.com/internal/integrations/redeem',{method:'POST',headers:{'content-type':'application/json',authorization:auth},body:JSON.stringify({client:'oscommerce',code:'c'.repeat(43)})});
|
||||
assert.equal((await handler(request('Bearer wrong'))).status,401);
|
||||
assert.equal((await handler(request('Bearer '+'x'.repeat(40)))).status,401);
|
||||
});
|
||||
Loading…
Reference in a new issue

Staff