fix(token): correct 'WWW-Authenticate' header in respose (#294)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Minghe · committed by GitHub · parent 081b5f7 · Browse files · 2 files·+18 added−10 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+18 added−10 removed
- tests:+12 added−4 removed
- other:+6 added−6 removed
Provenance
- Commit
e774e07- Tree
c442ddd- Author
Minghe <h.minghe@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
081b5f7
2 files changed+18 added−10 removed
modified:src/middleware/jwt/index.test.ts+12 added-4 removed
31 unmodified lines3233343536363738394025 unmodified lines66676869707071727374757677787980818283808485868788848990919293949531 unmodified lines it('Should not authorize', async () => { const req = new Request('http://localhost/auth/a') const res = await app.request(req) expect(res).not.toBeNull() expect(res.status).toBe(401) expect(res.status).toBe(400) expect(await res.text()).toBe('Unauthorized') expect(res.headers.get('x-foo')).toBeFalsy() })
25 unmodified lines it('Should not authorize Unicode', async () => { const invalidToken = 'ssyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJtZXNzYWdlIjoiaGVsbG8gd29ybGQifQ.B54pAqIiLbu170tGQ1rY06Twv__0qSHTA0ioQPIOvFE'
const req = new Request('http://localhost/auth-unicode/a') const url = 'http://localhost/auth-unicode/a' const req = new Request(url) req.headers.set('Authorization', `Basic ${invalidToken}`) const res = await app.request(req) expect(res).not.toBeNull() expect(res.status).toBe(401) expect(res.headers.get('www-authenticate')).toEqual( `Bearer realm="${url}",error="invalid_token",error_description="token verification failure"` ) expect(res.headers.get('x-foo')).toBeFalsy() })
it('Should not authorize', async () => { const invalid_token = 'invalid token' const req = new Request('http://localhost/auth/a') const url = 'http://localhost/auth/a' const req = new Request(url) req.headers.set('Authorization', `Bearer ${invalid_token}`) const res = await app.request(req) expect(res).not.toBeNull() expect(res.status).toBe(401) expect(res.status).toBe(400) expect(res.headers.get('www-authenticate')).toEqual( `Bearer realm="${url}",error="invalid_request",error_description="no authorization included in request"` ) expect(res.headers.get('x-foo')).toBeFalsy() })})modified:src/middleware/jwt/index.ts+6 added-6 removed
11 unmodified lines12131415161617181819202122232425262727282929303132339 unmodified lines43444546474748495051525253545511 unmodified lines const credentials = ctx.req.headers.get('Authorization')
if (!credentials) { ctx.res = new Response('Unauthorized', { status: 401, status: 400, headers: { 'WWW-Authenticate': 'Basic ${options.secret}', 'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="no authorization included in request"`, }, }) return }
const parts = credentials.split(/\s+/) if (parts.length !== 2) { ctx.res = new Response('Unauthorized', { status: 401, status: 400, headers: { 'WWW-Authenticate': 'Basic ${options.secret}', 'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_request",error_description="no authorization included in request"`, }, }) return }9 unmodified lines ctx.res = new Response('Unauthorized', { status: 401, statusText: msg, headers: { 'WWW-Authenticate': 'Bearer ${options.secret}', 'WWW-Authenticate': `Bearer realm="${ctx.req.url}",error="invalid_token",error_description="token verification failure"`, }, }) return }
await next() }}