Custom Authentication and Authorization Using Built in APEX Access Control - A How To. - Oracle Application Express Blog
Custom Authentication and Authorization Using Built in APEX Access Control - A How To. - Oracle Application Express Blog
APPLICATION EXPRESS
Custom Authentication
and Authorization using
built in APEX Access
Control - A How To.
Doug Gault
CONSULTING MEMBER OF TECHNICAL STAFF
Overview
APEX does a great job of providing an awful lot for
free, and that goes a long way provided that you
stay within the framework for which each component
was intended. But what if your case is special? (Isn't
everyones?). The customer I met with is building a
system that will be available to potentially anyone, so
they want to build a custom authentication scheme
where they can maintain users and their a;ributes.
Their system will be open to individuals as well as
organizations so, depending on their role, individual
users may have very di_erent authorities in the
system.
Custom Authentication
As I mentioned, there are various blog posts out there
on Authentication. But I don't want to send you
scurrying around the web scrubbing through multiple
posts, so I'll post the code for a simple custom
authentication scheme here.
-------------------------------------------------
-----------------------
-- FUNCTION: H A S H _ P A S S W O R D
-------------------------------------------------
-----------------------
dbms_obfuscation_toolkit.md5(
substr(l_salt,4,14) ||
p_user_name
||
substr(l_salt,5,10)));
return l_password;
end hash_password;
/
-------------------------------------------------
-----------------------
-- TABLE: U S E R S
-------------------------------------------------
-----------------------
CREATE SEQUENCE "USERS_SEQ" MINVALUE 1 MAXVALUE
999999999999999999999999999 INCREMENT BY 1 START
WITH 1 NOCACHE NOORDER NOCYCLE NOKEEP NOSCALE
GLOBAL
/
/
ALTER TRIGGER "BI_USERS" ENABLE
/
/
ALTER TRIGGER "BU_USERS" ENABLE
/
-------------------------------------------------
-----------------------
-- FUNCTION: A U T H E N T I C A T E _ U S E R
-------------------------------------------------
-----------------------
CREATE OR REPLACE FUNCTION AUTHENTICATE_USER
(p_username in varchar2,
p_password in varchar2)
return boolean
is
l_user_name users.user_name%type :=
upper(p_username);
l_password users.password%type;
l_hashed_password varchar2(1000);
l_count number;
begin
-- Returns from the AUTHENTICATE_USER function
-- 0 Normal, successful authentication
-- 1 Unknown User Name
-- 2 Account Locked
-- 3 Account Expired
-- 4 Incorrect Password
-- 5 Password First Use
-- 6 Maximum Login Attempts Exceeded
-- 7 Unknown Internal Error
--
-- First, check to see if the user exists
select count(*)
into l_count
from users
where user_name = l_user_name;
APEX_UTIL.SET_AUTHENTICATION_RESULT(0);
return true;
else
-- The Passwords didn't match
APEX_UTIL.SET_AUTHENTICATION_RESULT(4);
return false;
end if;
else
-- The username does not exist
APEX_UTIL.SET_AUTHENTICATION_RESULT(1);
return false;
end if;
-- If we get here then something weird
happened.
APEX_UTIL.SET_AUTHENTICATION_RESULT(7);
return false;
exception
when others then
-- We don't know what happened so log an
unknown internal error
APEX_UTIL.SET_AUTHENTICATION_RESULT(7);
-- And save the SQL Error Message to the
Auth Status.
APEX_UTIL.SET_CUSTOM_AUTH_STATUS(sqlerrm);
return false;
end authenticate_user;
/
1 Application SeFing
(ACCESS_CONTROL_SCOPE) - Used to dictate
whether Access control is enabled or not.
1 Build Option (Feature: Access Control) - Not
used unless you also choose to install the
Conaguration Options feature.
3 Roles in Application Access Control
(Administrator, Contributor, Reader) - Roles
used to exemplify a simple segregation of
duties.
1 User to Role Mapping - By default, when you
create the feature, APEX maps your current
APEX user to the Administrator role.
3 Authorization Schemes (Administrator
Rights, Contributor Rights, Reader Rights) -
These authorization schemes are based on the
roles created in Application Access Control and
can be used to lock down APEX components.
2 List of Values (ACCESS_ROLES,
EMAIL_USERNAME_FORMAT) - The arst is a
dynamic LOV that lists all of the current roles
deaned as part of Application Access Control.
The second is a simple LOV used on one of the
forms used to create multiple users.
6 Pages and their associated Menu,
Breadcrumb entries - An overall
Administration page, a page to conagure Access
Control, A report and form to maintain
individual users, and a 2 step wizard to add
multiple users.
Actually, that's quite a lot that the wizard does for you,
and as an example, its very useful. However, out of the
box, the feature isn't without its challenges. For
instance, there is no hard link between the usernames
you assign roles to and the users that are actually
deaned in the system. Meaning you could just make
up a user that doesn't exist and assign a role to it. And
while the default roles (and authorization schemes)
are great examples, most systems are more
complicated than that, requiring more granular roles
and varying level of authority.
1-Administrator
2-Manager
3-User
4-Consumer
Authorization Schemes
By themselves, the Access Control Roles don't, in
essence, do anything but provide the ability to link a
user to a role. It's the related Authorization Schemes
that provide the logic that allow you to tie a Role to a
given Component in APEX. Discussing how to
properly implement Authorization schemes can be a
rabbit hole, but in the interest of keeping things as
simple as possible, we're going to rely on as much
built in functionality as we can for our example.
Creating a UI
Now that we have all the back end components we
need, we can create a UI that allows us to test it all out.
For the purposes of demonstration I'm going to do
everything on one page.
declare
l_roles varchar2(4000);
begin
select role_ids
into l_roles
from apex_appl_acl_users
where APPLICATION_ID = :APP_ID
and USER_NAME = 'ADMIN';
--
:P1_ROLE_IDS := l_roles;
exception
when no_data_found then
:P10001_ROLE_ID := '';
end;
declare
l_roles wwv_flow_t_number :=
apex_string.split_numbers(:P1_ROLE_IDS, ':');
BEGIN
APEX_ACL.REPLACE_USER_ROLES (
p_application_id => :APP_ID,
p_user_name => 'ADMIN',
p_role_ids => l_roles );
END;
Summary
In this post we've explored the components of Access
Control and how to use them to implement a custom
Authorization Scheme using the built in components.
There is obviously a lot more you could do to extend
and demonstrate authorization techniques, but this
post is longer than even I thought it would be.
Recent Content
Site Map Legal Notices Terms of Use Privacy Preferências de cookies Ad Choices
Oracle Content Marketing Login