FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
users/src/Middleware/SocialAuthMiddleware.php at develop · CakeDC/users · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
CakeDC
/
users
Public
Notifications
You must be signed in to change notification settings
Fork
289
Star
523
Code
Issues
45
Pull requests
4
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
users
/
src
/
Middleware
/
SocialAuthMiddleware.php
Copy path
More file actions
More file actions
Latest commit
History
History
History
133 lines (117 loc) · 4.66 KB
Breadcrumbs
users
/
src
/
Middleware
/
SocialAuthMiddleware.php
Copy path
File metadata and controls
133 lines (117 loc) · 4.66 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
<?php
declare
(strict_types=
1
);
/**
* Copyright 2010 - 2019, Cake Development Corporation (https://www.cakedc.com)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2010 - 2018, Cake Development Corporation (https://www.cakedc.com)
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
namespace
CakeDC
\
Users
\
Middleware
;
use
Cake
\
Core
\
Configure
;
use
Cake
\
Http
\
Response
;
use
Cake
\
Http
\
ServerRequest
;
use
Cake
\
Log
\
LogTrait
;
use
Cake
\
Routing
\
Router
;
use
CakeDC
\
Auth
\
Social
\
Service
\
ServiceFactory
;
use
CakeDC
\
Users
\
Authenticator
\
SocialAuthenticator
;
use
CakeDC
\
Users
\
Exception
\
MissingEmailException
;
use
CakeDC
\
Users
\
Exception
\
SocialAuthenticationException
;
use
CakeDC
\
Users
\
Utility
\
UsersUrl
;
use
Psr
\
Http
\
Message
\
ResponseInterface
;
use
Psr
\
Http
\
Message
\
ServerRequestInterface
;
use
Psr
\
Http
\
Server
\
MiddlewareInterface
;
use
Psr
\
Http
\
Server
\
RequestHandlerInterface
;
class
SocialAuthMiddleware
implements
MiddlewareInterface
{
use
LogTrait;
/**
* Handle SocialAuthenticationException
*
* @param \Cake\Http\ServerRequest $request The request.
* @param \CakeDC\Users\Exception\SocialAuthenticationException $exception Exception thrown
* @return \Psr\Http\Message\ResponseInterface A response
*/
protected
function
onAuthenticationException
(
ServerRequest
$
request
,
$
exception
)
{
$
baseClassName
=
get_class
(
$
exception
->
getPrevious
());
$
response
=
new
Response
();
if
(
$
baseClassName
=== MissingEmailException::class) {
$
this
->
setErrorMessage
(
$
request
,
__d
(
'
cake_d_c/users
'
,
'
Please enter your email
'
));
$
request
->
getSession
()->
write
(
Configure::
read
(
'
Users.Key.Session.social
'
),
$
exception
->
getAttributes
()[
'
rawData
'
]
);
return
$
this
->
responseWithActionLocation
(
$
response
,
'
socialEmail
'
);
}
$
this
->
setErrorMessage
(
$
request
,
__d
(
'
cake_d_c/users
'
,
'
Could not identify your account, please try again
'
));
return
$
this
->
responseWithActionLocation
(
$
response
,
'
login
'
);
}
/**
* Set request error message
*
* @param \Cake\Http\ServerRequest $request the request with session attribute
* @param string $message the message
* @return void
*/
private
function
setErrorMessage
(
ServerRequest
$
request
,
$
message
)
{
$
messages
= (
array
)
$
request
->
getSession
()->
read
(
'
Flash.flash
'
);
$
messages
[] = [
'
key
'
=>
'
flash
'
,
'
element
'
=>
'
Flash/error
'
,
'
params
'
=> [],
'
message
'
=>
$
message
,
];
$
request
->
getSession
()->
write
(
'
Flash.flash
'
,
$
messages
);
}
/**
* Set location header to response using the string action
*
* @param \Cake\Http\Response $response to set location header
* @param string $action action at users controller
* @return \Psr\Http\Message\ResponseInterface
*/
protected
function
responseWithActionLocation
(
Response
$
response
,
$
action
)
{
$
url
= Router::
url
(UsersUrl::
actionUrl
(
$
action
));
return
$
response
->
withLocation
(
$
url
);
}
/**
* Go to next handling SocialAuthenticationException
*
* @param \Cake\Http\ServerRequest $request The request
* @param \Psr\Http\Server\RequestHandlerInterface $handler The request handler.
* @return \Psr\Http\Message\ResponseInterface
*/
protected
function
goNext
(
ServerRequestInterface
$
request
,
RequestHandlerInterface
$
handler
):
ResponseInterface
{
try
{
return
$
handler
->
handle
(
$
request
);
}
catch
(
SocialAuthenticationException
$
exception
) {
return
$
this
->
onAuthenticationException
(
$
request
,
$
exception
);
}
}
/**
* Callable implementation for the middleware stack.
*
* @param \Psr\Http\Message\ServerRequestInterface $request The request.
* @param \Psr\Http\Server\RequestHandlerInterface $handler The request handler.
* @return \Psr\Http\Message\ResponseInterface A response.
*/
public
function
process
(
ServerRequestInterface
$
request
,
RequestHandlerInterface
$
handler
):
ResponseInterface
{
if
(!(
new
UsersUrl
())->
checkActionOnRequest
(
'
socialLogin
'
,
$
request
)) {
return
$
handler
->
handle
(
$
request
);
}
$
service
= (
new
ServiceFactory
())->
createFromRequest
(
$
request
);
if
(!
$
service
->
isGetUserStep
(
$
request
)) {
return
(
new
Response
())
->
withLocation
(
$
service
->
getAuthorizationUrl
(
$
request
));
}
$
request
=
$
request
->
withAttribute
(SocialAuthenticator::
SOCIAL_SERVICE_ATTRIBUTE
,
$
service
);
return
$
this
->
goNext
(
$
request
,
$
handler
);
}
}
Back
|
FazBrowse Home
|
New Git URL