CREATE
OR
REPLACE
PACKAGE BODY pkg_apex_sec
AS
gc_delim CONSTANT VARCHAR2 (1) :=
'@'
;
/**
*
Returns
hashed value
* May require sys
to
grant
access
to
dbms_crypto
* -
GRANT
EXECUTE
ON
DBMS_CRYPTO
TO
<
schema
>;
* @param p_source
* @param p_key
* @
return
hashed value
*/
FUNCTION
f_get_md5 (
p_source
IN
VARCHAR2,
p_key
IN
VARCHAR2
)
RETURN
VARCHAR2
IS
v_key VARCHAR2 (4000) := p_key;
BEGIN
IF p_source = p_key
THEN
v_key := p_key || v ('APP_ID
');
END IF;
-- Can'
t use DBMS_CRYPTO
in
apex.oracle.com. Using generic coding
RETURN
p_source || p_key || 123;
END
f_get_md5;
/**
*
Returns
secure value
* @param p_value
* @
return
*/
FUNCTION
f_get_sec_val (
p_value
IN
VARCHAR2
)
RETURN
VARCHAR2
AS
BEGIN
RETURN
p_value || gc_delim || f_get_md5 (p_source => p_value, p_key => p_value || v (
'APP_ID'
));
END
f_get_sec_val;
/**
* Checks if secured value
is
valid
* @param p_hashed_val (
case
sensitive
* @
return
'Y'
or
'N'
*/
FUNCTION
is_valid_hashed_val (
p_hash
IN
VARCHAR2
)
RETURN
VARCHAR2
AS
v_value VARCHAR2 (4000);
BEGIN
v_value :=
REPLACE
(REGEXP_SUBSTR (p_hash,
'^[[:print:]]+'
|| gc_delim), gc_delim);
IF p_hash = f_get_sec_val (p_value => v_value)
THEN
RETURN
'Y'
;
ELSE
RETURN
'N'
;
END
IF;
EXCEPTION
WHEN
OTHERS
THEN
RETURN
'N'
;
END
is_valid_hashed_val;
/**
* unsecure value given the hash
* @param p_hash
* @
return
unsecure number
*/
FUNCTION
f_get_val (
p_hash
IN
VARCHAR2
)
RETURN
VARCHAR2
AS
BEGIN
IF is_valid_hashed_val (p_hash => p_hash) =
'N'
THEN
RETURN
NULL
;
END
IF;
RETURN
(
REPLACE
(REGEXP_SUBSTR (p_hash,
'^[[:print:]]+'
|| gc_delim), gc_delim));
EXCEPTION
WHEN
OTHERS
THEN
RETURN
NULL
;
END
f_get_val;
/**
* Sets unsec
values
in
the page given the secure
values
* @param p_page_id Page ID
to
set
.
Default
current
page
*/
PROCEDURE
sp_set_page_unsec_values (
p_page_id
IN
apex_application_pages.page_id%TYPE
DEFAULT
v (
'APP_PAGE_ID'
)
)
AS
v_app_id apex_applications.application_id%TYPE := v (
'APP_ID'
);
BEGIN
BEGIN
FOR
x
IN
(
SELECT
a1.item_name item_name_sec,
a2.item_name
FROM
apex_application_page_items a1,
apex_application_page_items a2
WHERE
a1.application_id = v_app_id
AND
a1.page_id = p_page_id
AND
a1.item_name
LIKE
'%_SEC'
AND
a2.application_id = a1.application_id
AND
a2.page_id = a1.page_id
AND
RTRIM (a1.item_name,
'_SEC'
) = a2.item_name) LOOP
apex_util.set_session_state (x.item_name, pkg_apex_sec.f_get_val (v (x.item_name_sec)));
END
LOOP;
END
;
END
sp_set_page_unsec_values;
/**
*
Set
all
the secure
values
given the unsecure
values
* @param p_page_id Page ID.
Default
current
page
*/
PROCEDURE
sp_set_page_sec_values (
p_page_id
IN
apex_application_pages.page_id%TYPE
DEFAULT
v (
'APP_PAGE_ID'
)
)
AS
v_app_id apex_applications.application_id%TYPE := v (
'APP_ID'
);
BEGIN
FOR
x
IN
(
SELECT
a1.item_name item_name_sec,
a2.item_name
FROM
apex_application_page_items a1,
apex_application_page_items a2
WHERE
a1.application_id = v_app_id
AND
a1.page_id = p_page_id
AND
a1.item_name
LIKE
'%_SEC'
AND
a2.application_id = a1.application_id
AND
a2.page_id = a1.page_id
AND
RTRIM (a1.item_name,
'_SEC'
) = a2.item_name) LOOP
IF v (x.item_name)
IS
NOT
NULL
THEN
apex_util.set_session_state (x.item_name_sec, pkg_apex_sec.f_get_sec_val (v (x.item_name)));
END
IF;
END
LOOP;
END
sp_set_page_sec_values;
END
pkg_apex_sec;
</
schema
>