Lua OpenAM client driver for the nginx HttpLuaModule.
Use OpenAM RESTful API.
It is different than an OpenAM agent.
0.0.4 released.
lua_package_cpath "/usr/lib64/lua/5.1/?.so;;";
lua_package_path "/usr/lib64/lua/5.1/resty/http/?.lua;/usr/share/lua/5.1/openam/?.lua;;";
server {
location /login.html {
access_by_lua '
local openam = require "openam"
local openam_uri = "http://openam.example.com:8080/openam"
local obj = openam.new(openam_uri, {name = "session"}, {success_url = false})
local status, json = obj:authenticate("my_login", "my_password")
-- Input can be escaped
-- local status, json = obj:authenticate(obj:escape_dn("my_login"), obj:escape_dn("my_password"))
if status == ngx.HTTP_OK then
-- session cookie added in the http response
return
end
-- do something
-- e.g. ngx.redirect(...), ngx.exit(...)
';
# proxy_pass/to/somewhere/...
}
location /resource.html {
access_by_lua '
local openam = require "openam"
local openam_uri = "http://openam.example.com:8080/openam"
local obj = openam.new(openam_uri, {name = "session"}, {success_url = false})
local status, json = obj:isTokenValid()
-- local status, json = obj:authorize()
if status == ngx.HTTP_OK then
return
end
-- do something
-- e.g. ngx.redirect(...), ngx.exit(...)
';
# proxy_pass/to/somewhere/...
}
location /logout.html {
access_by_lua '
local openam = require "openam"
local openam_uri = "http://openam.example.com:8080/openam"
local obj = openam.new(openam_uri, {name = "session"})
local status, json = obj:logout()
-- do something
-- e.g. ngx.redirect(...), ngx.exit(...)
-- session cookie removed in the http response
';
# proxy_pass/to/somewhere/...
}
location /resource.html {
access_by_lua '
local openam = require "openam"
local openam_uri = "http://openam.example.com:8080/openam"
local obj = openam.new(openam_uri, {name = "session"})
local status, json = obj:readIdentity("my_login")
if if status ~= ngx.HTTP_OK then
-- do something
-- e.g. ngx.redirect(...), ngx.exit(...)
end
';
# proxy_pass/to/somewhere/...
}
}
openam = openam.new(uri, cookie_params?, redirect_params?)
Creates the openam object. In case of failures, call ngx.exit
with HTTP_FORBIDDEN
status.
uri
: openam URI
The cookie_params
table accepts the following fields:
name
: string, cookie name between your app and nginx, default:openam_name
valueopenam_name
: string, cookie name between nginx and openam, default:iplanetDirectoryPro
domain
: string, cookie domain, default:host
secure
: boolean, cookie secure attribut, default:false
http_only
: boolean, cookie httpOnly attribut, default:true
path
: string, cookie path, default:/
The redirect_params
table accepts the following fields:
follow_success_url
: boolean, follow success url sent by OpenAM when authentication success, default:false
follow_failure_url
: boolean, follow failure url sent by OpenAM when authentication failed, default:false
status, json = openam:authenticate(username, password, realm?)
Authenticate an user.
Add a session cookie with the openam token.
username
: string, usernamepassword
: string, passwordrealm
: string, realm used for authentication, optional
Return:
status
: http status200
(authenticate) or401
(invalid password/username), callngx.exit
withHTTP_INTERNAL_SERVER_ERROR
if errorjson
: openam json response if status200
,nil
otherwise
status, json = openam:logout(token?)
Logout an user.
Remove the session cookie with the openam token.
token
: string, openam token, optional
Return:
status
: http status200
(logout), callngx.exit
withHTTP_INTERNAL_SERVER_ERROR
if errorjson
: openam json response if status200
,nil
otherwise
status, json = openam:logout(logout?, token?)
Check the validity of the token.
logout
: boolean, call logout if invalid token, optionaltoken
: string, openam token, optional
Return:
status
: http status200
(valid or sucess logout) or401
(invalid), callngx.exit
withHTTP_INTERNAL_SERVER_ERROR
if errorjson
: json response{"valid": true|false}
if status200
,nil
otherwise
status, json = openam:authorize(uri_value?, token?)
Check the access to an uri. In case of failures, call ngx.exit
with HTTP_FORBIDDEN
status.
uri_value
: string, uri to check, optional, default:scheme://host/uri
token
: string, openam token, optional
Return:
status
: response http statusjson
: nil if status not equal to 200 or 401 otherwise openam json response
status, json = openam:readIdentity(user, fields?, realm?, token?)
Read an identity. In case of failures, call ngx.exit
with HTTP_FORBIDDEN
status.
user
: string, usernamefields
: string separate by,
, selected fields, optionalrealm
: string, user realm, optionaltoken
: string, openam token, optional
Return:
status
: response http statusjson
: nil if status not equal to 200 otherwise openam json response
result = openam:escape_dn(s)
Escape some special LDAP character, prevent LDAP injection
s
: string
Return:
result
: escaped string
Lua OpenAM requires either Lua 5.1, Lua 5.2, or LuaJIT to build.
The build method can be selected from 4 options:
- Make
- RPM: Various Linux distributions
- LuaRocks (http://www.luarocks.org/): POSIX, OSX, Windows
The included Makefile
has generic settings.
First, review and update the included makefile to suit your platform (if required).
Next, install the module:
make install
Or install manually into your Lua module directory:
cp lib/openam/openam.lua $LUA_MODULE_DIRECTORY
Linux distributions using RPM can create a package via
the included RPM spec file. Ensure the +rpm-build+ package (or similar)
has been installed.
Build and install the module via RPM:
rpmbuild -tb 0.0.4.tar.gz
rpm -Uvh $LUA_OPENAM_RPM
LuaRocks can be used to install and manage Lua
modules on a wide range of platforms (including Windows).
First, extract the Lua OpenAM source package.
Next, install the module:
cd lua-openam-0.0.4
luarocks make
Gamaliel Sick
The MIT License (MIT)
Copyright (c) 2014 gsick
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.