Skip to content

Commit 141954f

Browse files
committed
feat: Migrate from login to sso
This required adding jumbojett/openid-connect-php as a dependency, as well as fixing the nginx configuration in the Docker Compose dev environment.
1 parent a18c8f0 commit 141954f

File tree

5 files changed

+279
-48
lines changed

5 files changed

+279
-48
lines changed

app/Http/Controllers/AuthController.php

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,31 @@
77
use GuzzleHttp\Client;
88

99
use 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
*/
1921
class 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
}

compose.yaml

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,22 @@ services:
88
- APP_KEY=12345678901234567890abcdefabcdea
99
- APP_DEBUG=true
1010
- APP_LOG_LEVEL=debug
11-
- APP_URL=http://localhost:8000/
11+
- APP_URL=http://localhost:8080/
1212
- DB_CONNECTION=pgsql
1313
- DB_DRIVER=pgsql
1414
- DB_HOST=db
1515
- DB_DATABASE=postgres
1616
- DB_USERNAME=postgres
1717
- DB_PASSWORD=postgres
18-
- LOGIN_API_URL=http://nyckeln:7002
19-
- LOGIN_FRONTEND_URL=http://localhost:7002
2018
- SSO_API_URL=http://nyckeln:7003
19+
- OIDC_PROVIDER=http://nyckeln:7003
20+
- OIDC_ID=client-id
21+
- OIDC_SECRET=client-secret
22+
- REDIRECT_URL=http://localhost:8080/login-complete
2123
- PORT=8080
24+
configs:
25+
- source: http_proxy.nginx.conf
26+
target: /etc/nginx/http.d/http_proxy.conf
2227
depends_on:
2328
db:
2429
condition: service_healthy
@@ -55,22 +60,23 @@ services:
5560
- 7004:7004
5661

5762
configs:
58-
nginx.conf:
63+
http_proxy.nginx.conf:
5964
content: |
60-
events {}
61-
62-
http {
63-
server {
64-
listen 7003;
65+
server {
66+
listen 7003;
6567
66-
location / {
67-
proxy_pass http://nyckeln:7003;
68-
}
68+
location / {
69+
proxy_pass http://nyckeln:7003;
6970
}
7071
}
7172
7273
nyckeln.yaml:
7374
content: |
75+
clients:
76+
- id: client-id
77+
secret: client-secret
78+
redirect_uris:
79+
- http://localhost:8080/login-complete
7480
users:
7581
- ug_kth_id: some-id
7682
kth_id: turetek

composer.json

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@
66
"type": "project",
77
"require": {
88
"php": ">=5.6.4",
9+
"ext-mbstring": "*",
910
"guzzlehttp/guzzle": "^6.3",
11+
"jumbojett/openid-connect-php": "^1.0",
1012
"laravel/framework": "5.4.*",
1113
"laravel/tinker": "~1.0",
12-
"netcarver/textile": "3.6.*",
13-
"ext-mbstring": "*"
14+
"netcarver/textile": "3.6.*"
1415
},
1516
"require-dev": {
1617
"fzaninotto/faker": "~1.4",
@@ -55,7 +56,10 @@
5556
"config": {
5657
"preferred-install": "dist",
5758
"sort-packages": true,
58-
"optimize-autoloader": true
59+
"optimize-autoloader": true,
60+
"allow-plugins": {
61+
"kylekatarnls/update-helper": true
62+
}
5963
},
6064
"extra": {
6165
"heroku": {

0 commit comments

Comments
 (0)