77use GuzzleHttp \Client ;
88
99use App \User ;
10- use Auth ;
11- use Session ;
10+ use Exception ;
11+ use Illuminate \Support \Facades \Auth ;
12+ use Illuminate \Support \Facades \Session ;
13+ use Jumbojett \OpenIDConnectClient ;
1214
1315/**
14- * Authentication controller. Handles login via login2 .datasektionen.se.
16+ * Authentication controller. Handles login via sso .datasektionen.se.
1517*
16- * @author Jonas Dahl <[email protected] > 17- * @version 2016 -11-23
18+ * @author Jonas Dahl <[email protected] >, Rasmus Söderhielm <[email protected] > 19+ * @version 2025 -11-10
1820*/
1921class AuthController {
2022 use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
2123
24+ private OpenIDConnectClient $ oidc ;
25+
26+ function __construct () {
27+ $ this ->oidc = new OpenIDConnectClient (
28+ env ('OIDC_PROVIDER ' ),
29+ env ('OIDC_ID ' ),
30+ env ('OIDC_SECRET ' )
31+ );
32+ $ this ->oidc ->setRedirectURL (env ('REDIRECT_URL ' ));
33+ }
34+
2235 /**
2336 * The logout url. Redirects to main page with success message.
2437 *
@@ -32,41 +45,30 @@ public function getLogout() {
3245 }
3346
3447 /**
35- * The login page. Just redirects to login2 .
48+ * The login page. Just redirects to sso .
3649 *
37- * @return redirect to login2 .datasektionen.se
50+ * @return redirect to sso .datasektionen.se
3851 */
3952 public function getLogin (Request $ request ) {
40- return redirect ( env ( ' LOGIN_FRONTEND_URL ' ) . ' /login?callback= ' . url ( ' /login-complete ' ) . ' / ' );
53+ return $ this -> oidc -> authenticate ( );
4154 }
4255
4356 /**
44- * When login is complete, login2 will redirect us here. Now verify the login.
57+ * When login is complete, sso will redirect us here. Now verify the login.
4558 *
46- * @param string $token the token from login2
4759 * @return redirect to main page or intended page
4860 */
49- public function getLoginComplete ($ token ) {
50- // Send get request to login server
51- $ client = new Client ();
52- $ res = $ client ->request ('GET ' , env ('LOGIN_API_URL ' ) . '/verify/ ' . $ token . '.json ' , [
53- 'form_params ' => [
54- 'format ' => 'json ' ,
55- 'api_key ' => env ('LOGIN_API_KEY ' )
56- ]
57- ]);
58-
59- // We now have a response. If it is good, parse the json and login user
60- if ($ res ->getStatusCode () == 200 ) {
61- $ body = json_decode ($ res ->getBody ());
62- $ user = User::createIfNotExistsOrFail ($ body ->user , $ body );
63-
64- Auth::login ($ user );
65- } else {
66- Auth::logout ();
61+ public function getLoginComplete () {
62+ if ($ this ->oidc ->authenticate () === FALSE ) {
6763 return redirect ('/ ' )->with ('error ' , 'Du loggades inte in. ' );
6864 }
6965
66+ $ kthId = $ this ->oidc ->getVerifiedClaims ('sub ' );
67+
68+ $ user = User::createIfNotExistsOrFail ($ kthId );
69+
70+ Auth::login ($ user );
71+
7072 return redirect ()->intended ('/ ' )->with ('success ' , 'Du loggades in. ' );
7173 }
7274}
0 commit comments