Skip to content
GitLab
Explore
Projects
Groups
Topics
Snippets
Projects
Groups
Topics
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
fusiondirectory
fusiondirectory-orchestrator
Commits
43f4997c
Commit
43f4997c
authored
1 month ago
by
Oana-Eliza Alexa
Browse files
Options
Download
Patches
Plain Diff
fix indent
parent
615347e7
dev
73-orchestrator-create-a-librabry-in-core-orchestrator
78-redesign-audit-class
79-redesign-mail
81-redesign-reminders-class
82-redesign-lifecycle-class
89-orchestrator-lifecycle-adaptation-to-regex-supann-selection
revert-55863b23
2 merge requests
!80
Resolve "[Orchestrator] - Create a librabry in core orchestrator"
,
!78
Draft: Resolve "Redesign lifecycle class"
Pipeline
#32347
failed with stages
in 18 seconds
Changes
1
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
library/TokenUtils.php
+181
-180
library/TokenUtils.php
with
181 additions
and
180 deletions
+181
-180
library/TokenUtils.php
+
181
−
180
View file @
43f4997c
...
@@ -2,196 +2,197 @@
...
@@ -2,196 +2,197 @@
class
TokenUtils
class
TokenUtils
{
{
private
function
__construct
()
{
private
function
__construct
()
{
}
/**
* @param string $userDN
* @param int $timeStamp
* @return string
* @throws Exception
*/
public
static
function
generateToken
(
string
$userDN
,
int
$timeStamp
):
string
{
$token
=
NULL
;
// Salt has been generated with APG.
$salt
=
'8onOlEsItKond'
;
$payload
=
json_encode
(
$userDN
.
$salt
);
// This allows the token to be different every time.
$time
=
time
();
// Create hmac with sha256 alg and the key provided for JWT token signature in ENV.
$token_hmac
=
hash_hmac
(
"sha256"
,
$time
.
$payload
,
$_ENV
[
"SECRET_KEY"
],
TRUE
);
// We need to have a token allowed to be used within an URL.
$token
=
Utils
::
base64urlEncode
(
$token_hmac
);
// Save token within LDAP
self
::
saveTokenInLdap
(
$userDN
,
$token
,
$timeStamp
);
return
$token
;
}
/**
* @param string $userDN
* @param string $token
* NOTE : UID is the full DN of the user. (uid=...).
* @param int $days
* @return bool
* @throws Exception
*/
public
static
function
saveTokenInLdap
(
string
$userDN
,
string
$token
,
int
$days
,
TaskGateway
$gateway
):
bool
{
$result
=
FALSE
;
$currentTimestamp
=
time
();
// Calculate the future timestamp by adding days to the current timestamp (We actually adds number of seconds).
$futureTimestamp
=
$currentTimestamp
+
(
$days
*
24
*
60
*
60
);
preg_match
(
'/uid=([^,]+),ou=/'
,
$userDN
,
$matches
);
$uid
=
$matches
[
1
];
$dn
=
'cn='
.
$uid
.
','
.
'ou=tokens'
.
','
.
$_ENV
[
"LDAP_BASE"
];
$ldap_entry
[
"objectClass"
]
=
[
'top'
,
'fdTokenEntry'
];
$ldap_entry
[
"fdTokenUserDN"
]
=
$userDN
;
$ldap_entry
[
"fdTokenType"
]
=
'reminder'
;
$ldap_entry
[
"fdToken"
]
=
$token
;
$ldap_entry
[
"fdTokenTimestamp"
]
=
$futureTimestamp
;
$ldap_entry
[
"cn"
]
=
$uid
;
// set the dn for the token, only take what's between "uid=" and ",ou="
// Verify if token ou branch exists
if
(
!
self
::
tokenBranchExist
(
'ou=tokens'
.
','
.
$_ENV
[
"LDAP_BASE"
]))
{
// Create the branch
self
::
createBranchToken
();
}
}
/**
// The user token DN creation
* @param string $userDN
$userTokenDN
=
'cn='
.
$uid
.
',ou=tokens'
.
','
.
$_ENV
[
"LDAP_BASE"
];
* @param int $timeStamp
// Verify if a token already exists for specified user and remove it to create new one correctly.
* @return string
if
(
self
::
tokenBranchExist
(
$userTokenDN
))
{
* @throws Exception
// Remove the user token
*/
self
::
removeUserToken
(
$userTokenDN
);
public
static
function
generateToken
(
string
$userDN
,
int
$timeStamp
):
string
{
$token
=
NULL
;
// Salt has been generated with APG.
$salt
=
'8onOlEsItKond'
;
$payload
=
json_encode
(
$userDN
.
$salt
);
// This allows the token to be different every time.
$time
=
time
();
// Create hmac with sha256 alg and the key provided for JWT token signature in ENV.
$token_hmac
=
hash_hmac
(
"sha256"
,
$time
.
$payload
,
$_ENV
[
"SECRET_KEY"
],
TRUE
);
// We need to have a token allowed to be used within an URL.
$token
=
Utils
::
base64urlEncode
(
$token_hmac
);
// Save token within LDAP
self
::
saveTokenInLdap
(
$userDN
,
$token
,
$timeStamp
);
return
$token
;
}
}
/**
// Add token to LDAP for specific UID
* @param string $userDN
try
{
* @param string $token
$result
=
ldap_add
(
$gateway
->
ds
,
$dn
,
$ldap_entry
);
// bool returned
* NOTE : UID is the full DN of the user. (uid=...).
}
catch
(
Exception
$e
)
{
* @param int $days
echo
json_encode
([
"Ldap Error - Token could not be saved!"
=>
"
$e
"
]);
// string returned
* @return bool
exit
;
* @throws Exception
*/
public
static
function
saveTokenInLdap
(
string
$userDN
,
string
$token
,
int
$days
,
TaskGateway
$gateway
):
bool
{
$result
=
FALSE
;
$currentTimestamp
=
time
();
// Calculate the future timestamp by adding days to the current timestamp (We actually adds number of seconds).
$futureTimestamp
=
$currentTimestamp
+
(
$days
*
24
*
60
*
60
);
preg_match
(
'/uid=([^,]+),ou=/'
,
$userDN
,
$matches
);
$uid
=
$matches
[
1
];
$dn
=
'cn='
.
$uid
.
','
.
'ou=tokens'
.
','
.
$_ENV
[
"LDAP_BASE"
];
$ldap_entry
[
"objectClass"
]
=
[
'top'
,
'fdTokenEntry'
];
$ldap_entry
[
"fdTokenUserDN"
]
=
$userDN
;
$ldap_entry
[
"fdTokenType"
]
=
'reminder'
;
$ldap_entry
[
"fdToken"
]
=
$token
;
$ldap_entry
[
"fdTokenTimestamp"
]
=
$futureTimestamp
;
$ldap_entry
[
"cn"
]
=
$uid
;
// set the dn for the token, only take what's between "uid=" and ",ou="
// Verify if token ou branch exists
if
(
!
self
::
tokenBranchExist
(
'ou=tokens'
.
','
.
$_ENV
[
"LDAP_BASE"
]))
{
// Create the branch
self
::
createBranchToken
();
}
// The user token DN creation
$userTokenDN
=
'cn='
.
$uid
.
',ou=tokens'
.
','
.
$_ENV
[
"LDAP_BASE"
];
// Verify if a token already exists for specified user and remove it to create new one correctly.
if
(
self
::
tokenBranchExist
(
$userTokenDN
))
{
// Remove the user token
self
::
removeUserToken
(
$userTokenDN
);
}
// Add token to LDAP for specific UID
try
{
$result
=
ldap_add
(
$gateway
->
ds
,
$dn
,
$ldap_entry
);
// bool returned
}
catch
(
Exception
$e
)
{
echo
json_encode
([
"Ldap Error - Token could not be saved!"
=>
"
$e
"
]);
// string returned
exit
;
}
return
$result
;
}
/**
* @param int $subTaskCall
* @param int $firstCall
* @param int $secondCall
* @return int
* Note : Simply return the difference between first and second call. (First call can be null).
*/
public
static
function
getTokenExpiration
(
int
$subTaskCall
,
int
$firstCall
,
int
$secondCall
):
int
{
// if firstCall is empty, secondCall is the timestamp expiry for the token.
$result
=
$secondCall
;
if
(
!
empty
(
$firstCall
))
{
// Verification if the subTask is the second reminder or the first reminder.
if
(
$subTaskCall
===
$firstCall
)
{
$result
=
$firstCall
-
$secondCall
;
}
}
return
$result
;
}
}
/**
return
$result
;
* @param $userTokenDN
}
* @return void
* Note : Simply remove the token for specific user DN
/**
*/
* @param int $subTaskCall
public
static
function
removeUserToken
(
$userTokenDN
,
TaskGateway
$gateway
):
void
* @param int $firstCall
{
* @param int $secondCall
// Add token to LDAP for specific UID
* @return int
try
{
* Note : Simply return the difference between first and second call. (First call can be null).
$result
=
ldap_delete
(
$gateway
->
ds
,
$userTokenDN
);
// bool returned
*/
}
catch
(
Exception
$e
)
{
public
static
function
getTokenExpiration
(
int
$subTaskCall
,
int
$firstCall
,
int
$secondCall
):
int
echo
json_encode
([
"Ldap Error - User token could not be removed!"
=>
"
$e
"
]);
// string returned
{
exit
;
// if firstCall is empty, secondCall is the timestamp expiry for the token.
}
$result
=
$secondCall
;
if
(
!
empty
(
$firstCall
))
{
// Verification if the subTask is the second reminder or the first reminder.
if
(
$subTaskCall
===
$firstCall
)
{
$result
=
$firstCall
-
$secondCall
;
}
}
}
/**
return
$result
;
* Create ou=pluginManager LDAP branch
}
* @throws Exception
*/
/**
public
static
function
createBranchToken
(
TaskGateway
$gateway
):
void
* @param $userTokenDN
{
* @return void
try
{
* Note : Simply remove the token for specific user DN
ldap_add
(
*/
$gateway
->
ds
,
'ou=tokens'
.
','
.
$_ENV
[
"LDAP_BASE"
],
public
static
function
removeUserToken
(
$userTokenDN
,
TaskGateway
$gateway
):
void
[
{
'ou'
=>
'tokens'
,
// Add token to LDAP for specific UID
'objectClass'
=>
'organizationalUnit'
,
try
{
]
$result
=
ldap_delete
(
$gateway
->
ds
,
$userTokenDN
);
// bool returned
);
}
catch
(
Exception
$e
)
{
}
catch
(
Exception
$e
)
{
echo
json_encode
([
"Ldap Error - User token could not be removed!"
=>
"
$e
"
]);
// string returned
exit
;
echo
json_encode
([
"Ldap Error - Impossible to create the token branch"
=>
"
$e
"
]);
// string returned
exit
;
}
}
}
}
/**
* @param string $token
/**
* @param array $mailTemplateForm
* Create ou=pluginManager LDAP branch
* @
param string $taskDN
* @
throws Exception
* @return array
*/
*/
public
static
function
createBranchToken
(
TaskGateway
$gateway
):
void
public
static
function
generateTokenUrl
(
string
$token
,
array
$mailTemplateForm
,
string
$taskDN
):
array
{
{
try
{
//Only take the cn of the main task name :
ldap_add
(
preg_match
(
'/cn=([^,]+),ou=/'
,
$taskDN
,
$matches
);
$gateway
->
ds
,
'ou=tokens'
.
','
.
$_ENV
[
"LDAP_BASE"
],
$taskName
=
$matches
[
1
];
[
'ou'
=>
'tokens'
,
// Remove the API URI
'objectClass'
=>
'organizationalUnit'
,
$cleanedUrl
=
preg_replace
(
'#/rest\.php/v1$#'
,
''
,
$_ENV
[
'FUSION_DIRECTORY_API_URL'
]);
]
$url
=
$cleanedUrl
.
'/accountProlongation.php?token='
.
$token
.
'&task='
.
$taskName
;
)
;
}
catch
(
Exception
$e
)
{
$mailTemplateForm
[
'body'
]
.
=
$url
;
echo
json_encode
([
"Ldap Error - Impossible to create the token branch"
=>
"
$e
"
]);
// string returned
return
$mailTemplateForm
;
exit
;
}
}
}
/**
* @param string $dn
/**
* @return bool
* @param string $token
* Note : Simply inspect if the branch for token is existing.
* @param array $mailTemplateForm
*/
* @param string $taskDN
public
static
function
tokenBranchExist
(
string
$dn
,
TaskGateway
$gateway
):
bool
* @return array
{
*/
$result
=
FALSE
;
public
static
function
generateTokenUrl
(
string
$token
,
array
$mailTemplateForm
,
string
$taskDN
):
array
{
try
{
//Only take the cn of the main task name :
$search
=
ldap_search
(
$gateway
->
ds
,
$dn
,
"(objectClass=*)"
);
preg_match
(
'/cn=([^,]+),ou=/'
,
$taskDN
,
$matches
);
// Check if the search was successful
$taskName
=
$matches
[
1
];
if
(
$search
)
{
// Get the number of entries found
// Remove the API URI
$entries
=
ldap_get_entries
(
$gateway
->
ds
,
$search
);
$cleanedUrl
=
preg_replace
(
'#/rest\.php/v1$#'
,
''
,
$_ENV
[
'FUSION_DIRECTORY_API_URL'
]);
$url
=
$cleanedUrl
.
'/accountProlongation.php?token='
.
$token
.
'&task='
.
$taskName
;
// If entries are found, set result to true
if
(
$entries
[
"count"
]
>
0
)
{
$mailTemplateForm
[
'body'
]
.
=
$url
;
$result
=
TRUE
;
}
return
$mailTemplateForm
;
}
}
}
catch
(
Exception
$e
)
{
$result
=
FALSE
;
/**
* @param string $dn
* @return bool
* Note : Simply inspect if the branch for token is existing.
*/
public
static
function
tokenBranchExist
(
string
$dn
,
TaskGateway
$gateway
):
bool
{
$result
=
FALSE
;
try
{
$search
=
ldap_search
(
$gateway
->
ds
,
$dn
,
"(objectClass=*)"
);
// Check if the search was successful
if
(
$search
)
{
// Get the number of entries found
$entries
=
ldap_get_entries
(
$gateway
->
ds
,
$search
);
// If entries are found, set result to true
if
(
$entries
[
"count"
]
>
0
)
{
$result
=
TRUE
;
}
}
}
return
$result
;
}
catch
(
Exception
$e
)
{
$result
=
FALSE
;
}
}
return
$result
;
}
}
}
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment
Menu
Explore
Projects
Groups
Topics
Snippets