How to Create Custom Authentication scheme in Oracle APEX

How to Create Custom Authentication scheme in Oracle APEX

In this post, we will see how to create custom authentication scheme in Oracle APEX.

Custom authentication enables users to log in to APEX application with username and password stored in custom database table. This will provide complete control over the authentication interface.

APEX application will validate username and password against data stored in database table and provide access to user.

Let us create custom authentication scheme in oracle APEX step by step:

Step1===> Create Database table to store user credentials including username and password.

Use below table creation script to create table.

  1. CREATE TABLE XX_APEX_CUSTOM_USERS (
  2. USER_ID NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY,
  3. USERNAME VARCHAR2(100) UNIQUE NOT NULL,
  4. PASSWORD VARCHAR2(255) NOT NULL,
  5. IS_ACTIVE CHAR(1) DEFAULT 'Y',
  6. FULL_NAME VARCHAR2(200)
  7. )

Navigate to SQL workshop —->SQL Commands

A screenshot of a computer

AI-generated content may be incorrect.

Copy and paste above table creation script under SQL commands and click on run.

A screenshot of a computer

AI-generated content may be incorrect.

Table is created.

Create Function to encrypt password using SHA-256 algorithm

  1. CREATE OR REPLACE FUNCTION xx_hash_password (
  2. p_password IN VARCHAR2
  3. ) RETURN VARCHAR2 IS
  4. l_hash_raw RAW(32);
  5. BEGIN
  6. l_hash_raw := dbms_crypto.hash(
  7. utl_i18n.string_to_raw(p_password, 'AL32UTF8'),
  8. dbms_crypto.hash_sh256
  9. );
  10. RETURN lower(rawtohex(l_hash_raw));
  11. END;
A computer screen shot of a program

AI-generated content may be incorrect.

If we are getting this error, then our schema doesn’t have access to DBMS_CRYPTO package.

Login with SYS user and provide grant to APEX schema using below command:

GRANT EXECUTE ON DBMS_CRYPTO TO your_schema;

A screenshot of a computer

AI-generated content may be incorrect.

Now try again once access is granted.

A computer screen shot of a computer

AI-generated content may be incorrect.

Insert some sample data for users with username and password.

  1. BEGIN
  2. INSERT INTO xx_apex_custom_users (
  3. username,
  4. password,
  5. is_active,
  6. full_name
  7. ) VALUES ( 'admin',
  8. xx_hash_password('admin123'),
  9. 'Y',
  10. 'Admin User' );
  11. INSERT INTO xx_apex_custom_users (
  12. username,
  13. password,
  14. is_active,
  15. full_name
  16. ) VALUES ( 'john',
  17. xx_hash_password('john@2024'),
  18. 'Y',
  19. 'John Doe' );
  20. INSERT INTO xx_apex_custom_users (
  21. username,
  22. password,
  23. is_active,
  24. full_name
  25. ) VALUES ( 'jane',
  26. xx_hash_password('jane@2024'),
  27. 'Y',
  28. 'Jane Smith' );
  29. END;
A screenshot of a computer

AI-generated content may be incorrect.

A screenshot of a computer

AI-generated content may be incorrect.

Step2===> Create New Authentication Scheme with custom type.

Navigate to Shared components—>Security—>Authentication scheme

A screenshot of a computer

AI-generated content may be incorrect.

Click on Create-to-create new authentication scheme.

A screenshot of a computer

AI-generated content may be incorrect.

Click on Next.

A screenshot of a computer

AI-generated content may be incorrect.

Provide valid authentication scheme name and scheme type as Custom.

Create PLSQL function with the below script:

  1. FUNCTION xx_custom_auth_scheme (
  2. p_username IN VARCHAR2,
  3. p_password IN VARCHAR2
  4. ) RETURN BOOLEAN IS
  5. l_stored_password VARCHAR2(100);
  6. BEGIN
  7. SELECT
  8. password
  9. INTO l_stored_password
  10. FROM
  11. xx_apex_custom_users
  12. WHERE
  13. upper(username) = upper(p_username);
  14. RETURN l_stored_password = xx_hash_password(p_password);
  15. EXCEPTION
  16. WHEN no_data_found THEN
  17. RETURN FALSE;
  18. END;

Provide same function name for Authentication Function Name field as shown below:

A screenshot of a computer

AI-generated content may be incorrect.

A screenshot of a computer

AI-generated content may be incorrect.

Click on the Create Authentication scheme.

A screenshot of a computer

AI-generated content may be incorrect.

Once authentication is created, we need to make an authentication scheme as current authentication scheme.

Open XX_CUSTOM_AUTH_SCHEME and click on “Make Current Scheme”.

A screenshot of a computer

AI-generated content may be incorrect.

A screenshot of a computer

AI-generated content may be incorrect.

Click on OK.

A screenshot of a computer

AI-generated content may be incorrect.

Step3: Run the application.

A screenshot of a login form

AI-generated content may be incorrect.

Provide username as “john” and password as “john@2024” which we have inserted into table earlier.

Click on Sign in.

A close up of a screen

AI-generated content may be incorrect.

Here we can see, application is logged in with john.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top