8 namespace Fossology\UI\Page;
13 use League\OAuth2\Client\Provider\Exception\IdentityProviderException;
14 use League\OAuth2\Client\Provider\GenericProvider;
15 use League\OAuth2\Client\Token\AccessToken;
16 use League\OAuth2\Client\Token\AccessTokenInterface;
17 use Symfony\Component\HttpFoundation\Request;
18 use Symfony\Component\HttpFoundation\Response;
27 function __construct()
29 parent::__construct(self::NAME, array(
30 self::TITLE => _(
"Getting Started with FOSSology"),
31 self::REQUIRES_LOGIN =>
false,
32 self::MENU_LIST =>
"Home",
33 self::MENU_ORDER => 100
41 protected function handle(Request $request)
45 $vars = array(
'isSecure' => $request->isSecure());
46 $vars[
'loginProvider'] =
"password";
47 if (array_key_exists(
'AUTHENTICATION', $SysConf) &&
48 array_key_exists(
'provider', $SysConf[
'AUTHENTICATION'])) {
49 $vars[
'loginProvider'] = $SysConf[
'AUTHENTICATION'][
'provider'];
51 if (array_key_exists(
'User', $_SESSION) && $_SESSION[
'User'] ==
52 "Default User" && plugin_find_id(
"auth") >= 0) {
53 if (isset($_SERVER[
'HTTPS']) && $_SERVER[
'HTTPS'] !=
"off") {
54 $vars[
'protocol'] =
"HTTPS";
56 $vars[
'protocol'] = preg_replace(
"@/.*@",
"",
57 @$_SERVER[
'SERVER_PROTOCOL']);
60 $vars[
'referrer'] =
"?mod=browse";
61 $vars[
'authUrl'] =
"?mod=auth";
63 $vars[
'getEmail'] =
"";
64 $vars[
'getOauth'] =
false;
70 }
catch (IdentityProviderException $e) {
71 $vars[
'message'] = $e->getMessage();
72 }
catch (\UnexpectedValueException $e) {
73 $vars[
'message'] = $e->getMessage();
80 if ($email !==
null) {
81 $_SESSION[
'oauthemail'] = $email;
82 $vars[
'getOauth'] =
true;
83 if (array_key_exists(
'HTTP_REFERER', $_SESSION)) {
84 $vars[
'referrer'] = $_SESSION[
'HTTP_REFERER'];
88 if (!empty($SysConf[
'SYSCONFIG'][
'OidcAppName'])) {
89 $vars[
'providerExist'] = $SysConf[
'SYSCONFIG'][
'OidcAppName'];
91 $vars[
'providerExist'] = 0;
93 return $this->
render(
"home.html.twig", $this->mergeWithDefault($vars));
109 (isset($_SESSION[
'oauth2state']) &&
112 if (isset($_SESSION[
'oauth2state'])) {
113 unset($_SESSION[
'oauth2state']);
115 throw new \UnexpectedValueException(
'Invalid state');
118 if (array_key_exists(
'http_proxy', $SysConf[
'FOSSOLOGY']) &&
119 ! empty($SysConf[
'FOSSOLOGY'][
'http_proxy'])) {
120 $proxy = $SysConf[
'FOSSOLOGY'][
'http_proxy'];
122 if (array_key_exists(
'https_proxy', $SysConf[
'FOSSOLOGY']) &&
123 ! empty($SysConf[
'FOSSOLOGY'][
'https_proxy'])) {
124 $proxy = $SysConf[
'FOSSOLOGY'][
'https_proxy'];
127 $provider =
new GenericProvider([
128 "clientId" => $SysConf[
'SYSCONFIG'][
'OidcAppId'],
129 "clientSecret" => $SysConf[
'SYSCONFIG'][
'OidcSecret'],
130 "redirectUri" => $SysConf[
'SYSCONFIG'][
'OidcRedirectURL'],
131 "urlAuthorize" => $SysConf[
'SYSCONFIG'][
'OidcAuthorizeURL'],
132 "urlAccessToken" => $SysConf[
'SYSCONFIG'][
'OidcAccessTokenURL'],
133 "urlResourceOwnerDetails" => $SysConf[
'SYSCONFIG'][
'OidcResourceURL'],
134 "responseResourceOwnerId" => $SysConf[
'SYSCONFIG'][
'OidcResourceOwnerId'],
137 $accessToken = $provider->getAccessToken(
'authorization_code',
140 $this->validateAccessToken($accessToken);
153 private function validateAccessToken($accessToken)
160 $authHelper = $this->container->get(
'helper.authHelper');
161 $jwks = $authHelper::loadJwks();
163 if ($SysConf[
'SYSCONFIG'][
'OidcTokenType'] ===
"A") {
164 $jwtToken = $accessToken->getToken();
165 } elseif ($SysConf[
'SYSCONFIG'][
'OidcTokenType'] ===
"I") {
166 $jwtToken = $accessToken->getValues()[
'id_token'];
168 if (empty($jwtToken)) {
169 throw new \UnexpectedValueException(
"Unable to get identity from OIDC token. " .
170 "Please check 'Token to use from provider' field in config.");
173 $jwtTokenDecoded = JWT::decode(
177 }
catch (\Exception $e) {
178 throw new \UnexpectedValueException(
"JWKS: " . $e->getMessage());
180 if (property_exists($jwtTokenDecoded,
'iss') &&
181 $jwtTokenDecoded->{
'iss'} == $SysConf[
'SYSCONFIG'][
'OidcIssuer']) {
184 throw new \UnexpectedValueException(
"Invalid issuer of token.");
196 $resourceOwner = $provider->getResourceOwner($accessToken);
197 if (!empty($resourceOwner->getId())) {
198 return $resourceOwner->getId();
204 register_plugin(
new HomePage());
render($templateName, $vars=null, $headers=null)
Provides helper methods for REST api.
getEmailFromResource($provider, $accessToken)
GetParm($parameterName, $parameterType)
This function will retrieve the variables and check data types.