Skip to content

Commit

Permalink
Add missing config variable redirect_uri
Browse files Browse the repository at this point in the history
It's already used in ngx-oauth-userauth, but I forgot to implement it.
  • Loading branch information
jirutka committed Nov 20, 2015
1 parent ca50161 commit 2e8b90b
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
20 changes: 17 additions & 3 deletions spec/ngx-oauth/config_spec.moon
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,15 @@ describe 'load', ->
}

before_each ->
_G.ngx = { var: {} }
_G.ngx =
var: { scheme: 'https', server_name: 'example.org' }


context 'when all variables are set', ->
expected = merge {
scope: 'read'
redirect_path: 'http://example.rg'
redirect_path: '/callback'
redirect_uri: 'https://example.cz/oauth/callback'
server_url: 'not-used'
success_path: '/app/home'
cookie_path: '/app'
Expand All @@ -45,6 +47,7 @@ describe 'load', ->
scope: ''
server_url: ''
redirect_path: '/_oauth/callback'
redirect_uri: 'https://example.org/_oauth/callback'
success_path: '/'
cookie_path: '/'
cookie_prefix: 'oauth_'
Expand All @@ -63,6 +66,16 @@ describe 'load', ->
assert.is_falsy errs


context 'when ngx.var.oauth_redirect_uri is not set', ->
before_each ->
_G.ngx.var =
scheme: 'http', server_name: 'example.cz', oauth_redirect_path: '/callme'

it 'sets redirect_uri built from vars scheme, server_name and oauth_redirect_path', ->
actual = config.load()
assert.same 'http://example.cz/callme', actual.redirect_uri


context 'when ngx.var.oauth_server_url is set', ->
before_each ->
_G.ngx.var.oauth_server_url = 'http://example.org'
Expand Down Expand Up @@ -104,7 +117,8 @@ describe 'load', ->

context 'when ngx.var.oauth_client_secret is too short', ->
before_each ->
_G.ngx.var = { oauth_client_secret: '123', oauth_aes_bits: 128 }
_G.ngx.var.oauth_client_secret = '123'
_G.ngx.var.oauth_aes_bits = 128

it 'returns error message as 2nd value', ->
_, errs = config.load()
Expand Down
5 changes: 5 additions & 0 deletions src/ngx-oauth/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ local DEFAULTS = {
client_secret = '',
scope = '',
redirect_path = '/_oauth/callback',
redirect_uri = '',
server_url = '', -- used only as a shorthand for setting these 3 below
authorization_url = '${server_url}/authorize',
token_url = '${server_url}/token',
Expand Down Expand Up @@ -72,6 +73,10 @@ local M = {}
function M.load ()
local conf = load_from_ngx()

if is_blank(conf.redirect_uri) then
conf.redirect_uri = ngx.var.scheme..'://'..ngx.var.server_name..conf.redirect_path
end

if not is_blank(conf.server_url) then
for _, key in ipairs(OAAS_ENDPOINT_VARS) do
conf[key] = conf[key]:gsub('${server_url}', conf.server_url)
Expand Down

0 comments on commit 2e8b90b

Please sign in to comment.