-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathShieldOAuth.php
More file actions
148 lines (128 loc) · 5.57 KB
/
Copy pathShieldOAuth.php
File metadata and controls
148 lines (128 loc) · 5.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<?php
declare(strict_types=1);
/**
* This file is part of Shield OAuth.
*
* (c) Datamweb <pooya_parsa_dadashi@yahoo.com>
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Datamweb\ShieldOAuth\Libraries\Basic;
use CodeIgniter\Config\Factories;
use CodeIgniter\Files\FileCollection;
use Config\Autoload;
class ShieldOAuth
{
/**
* --------------------------------------------------------------------
* Set OAuth
* --------------------------------------------------------------------
* This function creating instances of OAuth class chosen by the user
*
* This function has the ability to identify classes in two Paths:
*
* - \App\Libraries\ShieldOAuth\ for load any OAuth custom class created by the user
* - \Datamweb\ShieldOAuth\Libraries\ for load GithubOAuth or GoogleOAuth class
*/
public static function setOAuth(string $serviceName): object
{
$serviceName = ucfirst($serviceName);
$className = '\Datamweb\ShieldOAuth\Libraries\\' . $serviceName . 'OAuth';
// For detect custom OAuth
if (file_exists(APPPATH . 'Libraries/ShieldOAuth' . DIRECTORY_SEPARATOR . $serviceName . 'OAuth.php')) {
$className = '\App\Libraries\ShieldOAuth\\' . $serviceName . 'OAuth';
}
return Factories::loadOAuth($className); // @phpstan-ignore-line
}
/**
* --------------------------------------------------------------------
* Names of all supported OAuth services
* --------------------------------------------------------------------
* List of all the OAuth services
*
* Identifies all OAuth classes and generates a string according to the file name of the classes as follows.
* e.g. 'github|google|yahoo' ...
*/
public function allOAuth(): string
{
$files = new FileCollection();
/** @var Autoload $autoload */
$autoload = new Autoload();
// Checking if it is installed manually
if (array_key_exists('Datamweb\ShieldOAuth', $autoload->psr4)) {
// Adds all Libraries files
$files = $files->add(APPPATH . 'ThirdParty/shield-oauth/src/Libraries', false);
} else {
// Adds all Libraries files if install via composer
$files = $files->add(__DIR__ . '/..', false);
}
// For to detect custom OAuth
if (is_dir(APPPATH . 'Libraries/ShieldOAuth')) {
$files = $files->add(APPPATH . 'Libraries/ShieldOAuth', false);
}
// show only all *OAuth.php files
$files = $files->retainPattern('*OAuth.php');
$allOAuth = '';
foreach ($files as $file) {
// make string github|google and ... from class name
$allOAuth .= strtolower(str_replace('OAuth.php', '|', $file->getBasename()));
}
return mb_substr($allOAuth, 0, -1);
}
/**
* --------------------------------------------------------------------
* Names of all Custom OAuth
* --------------------------------------------------------------------
* List of all just CustomOAuth services
*
* Returns array of all Custom OAuth classes file name
* e.g. ['yahoo']
*
* @return array<int, string>
*/
private function otherOAuth()
{
$pieces = explode('|', $this->allOAuth());
return array_diff($pieces, ['github', 'google']);
}
/**
* --------------------------------------------------------------------
* Make OAuth Button
* --------------------------------------------------------------------
* Creates OAuth buttons based on existing OAuth classes
*
* Group a series of buttons together on a single line by bootstrap 5.0
*
* @see https://getbootstrap.com/docs/5.0/components/button-group/#nesting
* @see https://getbootstrap.com/docs/5.0/components/button-group/#basic-example
*/
public function makeOAuthButton(string $forPage = 'login'): string
{
$active_by = lang('ShieldOAuthLang.login_by');
if ($forPage === 'register') {
$active_by = lang('ShieldOAuthLang.register_by');
}
$Button = "<hr>
<div class='text-center'>
<div class='btn-group' role='group' aria-label='Button group with nested dropdown'>
<a href='#' class='btn btn-primary active' aria-current='page'>" . $active_by . '</a>';
$Button .= '<a href=' . base_url('oauth/google') . " class='btn btn-outline-secondary' aria-current='page'>" . lang('ShieldOAuthLang.Google.google') . '</a>';
$Button .= '<a href=' . base_url('oauth/github') . " class='btn btn-outline-secondary' aria-current='page'>" . lang('ShieldOAuthLang.Github.github') . '</a>';
if ($this->otherOAuth() !== []) {
$Button .= "<div class='btn-group' role='group'>
<button type='button' class='btn btn-outline-secondary dropdown-toggle' data-bs-toggle='dropdown' aria-expanded='false'>"
. lang('ShieldOAuthLang.other') . "
</button>
<ul class='dropdown-menu'>";
foreach ($this->otherOAuth() as $oauthName) {
$OAuthName = ucfirst($oauthName);
$Button .= "<li><a class='dropdown-item' href=" . base_url("oauth/{$oauthName}") . '>' . lang("ShieldOAuthLang.{$OAuthName}.{$oauthName}") . '</a></li>';
}
$Button .= '</ul></div>';
}
return $Button . '
</div>
</div>';
}
}