Skip to content
Projects
Groups
Snippets
Help
Sign in / Register
Toggle navigation
Minds Backend - Engine
Project overview
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Locked Files
Issues
281
Merge Requests
39
CI / CD
Security & Compliance
Packages
Wiki
Snippets
Members
Collapse sidebar
Close sidebar
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Minds
Minds Backend - Engine
Compare Revisions
3ad92c52dfc11d8653c5f3f95340e0f201385052...c1c6569aeb5c310c189d31b88088acc39741e299
Source
c1c6569aeb5c310c189d31b88088acc39741e299
...
Target
3ad92c52dfc11d8653c5f3f95340e0f201385052
Compare
Commits (2)
(fix): Constant typo
· 2dd9da6b
Emiliano Balbuena
authored
31 minutes ago
2dd9da6b
(refactor): Domain check to Manager
· c1c6569a
Emiliano Balbuena
authored
17 minutes ago
c1c6569a
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
65 additions
and
76 deletions
+65
-76
Controllers/api/v2/sso/authorize.php
View file @
c1c6569a
...
...
@@ -54,22 +54,15 @@ class authorize implements Interfaces\Api, Interfaces\ApiIgnorePam
$sso
->
setDomain
(
$domain
);
if
(
!
$sso
->
isAllowed
())
{
return
Factory
::
response
([
'status'
=>
'error'
,
'message'
=>
'Domain not allowed'
,
]);
}
$jwt
=
$_POST
[
'token'
];
$success
=
$sso
->
authorize
(
$jwt
);
try
{
$sso
->
authorize
(
$_POST
[
'token'
]);
}
catch
(
Exception
$e
)
{
error_log
((
string
)
$e
);
if
(
!
$success
)
{
return
Factory
::
response
([
'status'
=>
'error'
,
'message'
=>
'
Invalid token
'
,
'message'
=>
'
Cannot authorize
'
,
]);
}
...
...
This diff is collapsed.
Controllers/api/v2/sso/connect.php
View file @
c1c6569a
...
...
@@ -52,16 +52,18 @@ class connect implements Interfaces\Api, Interfaces\ApiIgnorePam
$sso
->
setDomain
(
$domain
);
if
(
!
$sso
->
isAllowed
())
{
try
{
return
Factory
::
response
([
'token'
=>
$sso
->
generateToken
()
]);
}
catch
(
Exception
$e
)
{
error_log
((
string
)
$e
);
return
Factory
::
response
([
'status'
=>
'error'
,
'message'
=>
'
Domain not allowed'
'message'
=>
'
Cannot connect'
,
]);
}
return
Factory
::
response
([
'token'
=>
$sso
->
generateToken
()
]);
}
/**
...
...
This diff is collapsed.
Core/SSO/Manager.php
View file @
c1c6569a
...
...
@@ -16,7 +16,7 @@ use Minds\Core\Sessions\Manager as SessionsManager;
class
Manager
{
/** @var int */
const
J
TW
_EXPIRE
=
300
;
const
J
WT
_EXPIRE
=
300
;
/** @var Config */
protected
$config
;
...
...
@@ -71,7 +71,7 @@ class Manager
/**
* @return bool
*/
p
ublic
function
isAllowed
()
:
bool
p
rotected
function
isAllowed
()
:
bool
{
if
(
$this
->
proDelegate
->
isAllowed
(
$this
->
domain
))
{
return
true
;
...
...
@@ -86,6 +86,10 @@ class Manager
*/
public
function
generateToken
()
:
?
string
{
if
(
!
$this
->
isAllowed
())
{
throw
new
Exception
(
'Invalid domain'
);
}
$now
=
time
();
$session
=
$this
->
sessions
->
getSession
();
...
...
@@ -109,23 +113,27 @@ class Manager
->
encode
([
'key'
=>
$ssoKey
,
'domain'
=>
$this
->
domain
,
],
$now
,
$now
+
static
::
J
TW
_EXPIRE
);
],
$now
,
$now
+
static
::
J
WT
_EXPIRE
);
$this
->
cache
->
set
(
$ssoKey
,
$sessionToken
,
static
::
J
TW
_EXPIRE
*
2
);
->
set
(
$ssoKey
,
$sessionToken
,
static
::
J
WT
_EXPIRE
*
2
);
return
$jwt
;
}
/**
* @param string $jwt
* @return
bool
* @return
void
* @throws Exception
*/
public
function
authorize
(
string
$jwt
)
:
bool
public
function
authorize
(
string
$jwt
)
:
void
{
if
(
!
$jwt
)
{
return
false
;
throw
new
Exception
(
'Invalid JTW'
);
}
if
(
!
$this
->
isAllowed
())
{
throw
new
Exception
(
'Invalid domain'
);
}
$key
=
$this
->
config
->
get
(
'oauth'
)[
'encryption_key'
]
??
''
;
...
...
@@ -134,33 +142,26 @@ class Manager
throw
new
Exception
(
'Invalid encryption key'
);
}
try
{
$data
=
$this
->
jwt
->
setKey
(
$key
)
->
decode
(
$jwt
);
if
(
$this
->
domain
!==
$data
[
'domain'
])
{
throw
new
Exception
(
'Invalid domain'
);
}
$data
=
$this
->
jwt
->
setKey
(
$key
)
->
decode
(
$jwt
);
$ssoKey
=
$data
[
'key'
];
if
(
$this
->
domain
!==
$data
[
'domain'
])
{
throw
new
Exception
(
'Domain mismatch'
);
}
$sessionToken
=
$this
->
cache
->
get
(
$ssoKey
);
$ssoKey
=
$data
[
'key'
];
if
(
$sessionToken
)
{
$this
->
sessions
->
withString
(
$sessionToken
)
->
save
();
$sessionToken
=
$this
->
cache
->
get
(
$ssoKey
);
$this
->
cache
->
destroy
(
$ssoKey
);
}
if
(
$sessionToken
)
{
$this
->
sessions
->
withString
(
$sessionToken
)
->
save
();
return
true
;
}
catch
(
Exception
$e
)
{
error_log
((
string
)
$e
);
return
false
;
$this
->
cache
->
destroy
(
$ssoKey
);
}
}
}
This diff is collapsed.
Spec/Core/SSO/ManagerSpec.php
View file @
c1c6569a
...
...
@@ -2,6 +2,7 @@
namespace
Spec\Minds\Core\SSO
;
use
Exception
;
use
Minds\Common\Jwt
;
use
Minds\Core\Config
;
use
Minds\Core\Data\cache\abstractCacher
;
...
...
@@ -61,33 +62,13 @@ class ManagerSpec extends ObjectBehavior
$this
->
shouldHaveType
(
Manager
::
class
);
}
public
function
it_should_check_if_allowed
()
{
public
function
it_should_generate_token
(
Session
$session
)
{
$this
->
proDelegate
->
isAllowed
(
'phpspec.test'
)
->
shouldBeCalled
()
->
willReturn
(
true
);
$this
->
setDomain
(
'phpspec.test'
)
->
isAllowed
()
->
shouldReturn
(
true
);
}
public
function
it_should_check_if_not_allowed
()
{
$this
->
proDelegate
->
isAllowed
(
'phpspec.test'
)
->
shouldBeCalled
()
->
willReturn
(
false
);
$this
->
setDomain
(
'phpspec.test'
)
->
isAllowed
()
->
shouldReturn
(
false
);
}
public
function
it_should_generate_token
(
Session
$session
)
{
$this
->
sessions
->
getSession
()
->
shouldBeCalled
()
->
willReturn
(
$session
);
...
...
@@ -134,6 +115,10 @@ class ManagerSpec extends ObjectBehavior
public
function
it_should_not_generate_a_token_if_logged_out
()
{
$this
->
proDelegate
->
isAllowed
(
'phpspec.test'
)
->
shouldBeCalled
()
->
willReturn
(
true
);
$this
->
sessions
->
getSession
()
->
shouldBeCalled
()
->
willReturn
(
null
);
...
...
@@ -146,6 +131,10 @@ class ManagerSpec extends ObjectBehavior
public
function
it_should_authorize
()
{
$this
->
proDelegate
->
isAllowed
(
'phpspec.test'
)
->
shouldBeCalled
()
->
willReturn
(
true
);
$this
->
jwt
->
setKey
(
'~key~'
)
->
shouldBeCalled
()
->
willReturn
(
$this
->
jwt
);
...
...
@@ -175,12 +164,16 @@ class ManagerSpec extends ObjectBehavior
$this
->
setDomain
(
'phpspec.test'
)
->
authorize
(
'~jwt~'
)
->
shouldReturn
(
true
);
->
shouldNotThrow
(
Exception
::
class
)
->
duringAuthorize
(
'~jwt~'
);
}
public
function
it_should_not_authorize_if_domain_mismatches
()
{
$this
->
proDelegate
->
isAllowed
(
'other-phpspec.test'
)
->
shouldBeCalled
()
->
willReturn
(
true
);
$this
->
jwt
->
setKey
(
'~key~'
)
->
shouldBeCalled
()
->
willReturn
(
$this
->
jwt
);
...
...
@@ -196,8 +189,8 @@ class ManagerSpec extends ObjectBehavior
->
shouldNotBeCalled
();
$this
->
setDomain
(
'
phpspec-invalid
.test'
)
->
authorize
(
'~jwt~'
)
->
shouldReturn
(
false
);
->
setDomain
(
'
other-phpspec
.test'
)
->
shouldThrow
(
new
Exception
(
'Domain mismatch'
)
)
->
duringAuthorize
(
'~jwt~'
);
}
}
This diff is collapsed.