Skip to content

Commit 15e141c

Browse files
committed
Move to SspBridge in AuthContextService
1 parent 913962e commit 15e141c

File tree

5 files changed

+69
-21
lines changed

5 files changed

+69
-21
lines changed

src/Bridges/SspBridge/Utils.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace SimpleSAML\Module\oidc\Bridges\SspBridge;
66

7+
use SimpleSAML\Utils\Attributes;
78
use SimpleSAML\Utils\Auth;
89
use SimpleSAML\Utils\Config;
910
use SimpleSAML\Utils\HTTP;
@@ -15,6 +16,7 @@ class Utils
1516
protected static ?HTTP $http = null;
1617
protected static ?Random $random = null;
1718
protected static ?Auth $auth = null;
19+
protected static ?Attributes $attributes = null;
1820

1921
public function config(): Config
2022
{
@@ -35,4 +37,9 @@ public function auth(): Auth
3537
{
3638
return self::$auth ??= new Auth();
3739
}
40+
41+
public function attributes(): Attributes
42+
{
43+
return self::$attributes ??= new Attributes();
44+
}
3845
}

src/Services/AuthContextService.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,9 @@
66

77
use RuntimeException;
88
use SimpleSAML\Auth\Simple;
9+
use SimpleSAML\Module\oidc\Bridges\SspBridge;
910
use SimpleSAML\Module\oidc\Factories\AuthSimpleFactory;
1011
use SimpleSAML\Module\oidc\ModuleConfig;
11-
use SimpleSAML\Utils\Attributes;
12-
use SimpleSAML\Utils\Auth;
1312

1413
/**
1514
* Provide contextual authentication information for administration interface.
@@ -28,13 +27,13 @@ class AuthContextService
2827
public function __construct(
2928
private readonly ModuleConfig $moduleConfig,
3029
private readonly AuthSimpleFactory $authSimpleFactory,
30+
private readonly SspBridge $sspBridge,
3131
) {
3232
}
3333

3434
public function isSspAdmin(): bool
3535
{
36-
// TODO mivanci make bridge to SSP utility classes (search for SSP namespace through the codebase)
37-
return (new Auth())->isAdmin();
36+
return $this->sspBridge->utils()->auth()->isAdmin();
3837
}
3938

4039
/**
@@ -45,7 +44,10 @@ public function getAuthUserId(): string
4544
{
4645
$simple = $this->authenticate();
4746
$userIdAttr = $this->moduleConfig->getUserIdentifierAttribute();
48-
return (string)(new Attributes())->getExpectedAttribute($simple->getAttributes(), $userIdAttr);
47+
return (string)$this->sspBridge->utils()->attributes()->getExpectedAttribute(
48+
$simple->getAttributes(),
49+
$userIdAttr,
50+
);
4951
}
5052

5153
/**

src/Services/Container.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,15 +131,19 @@ public function __construct()
131131
$authSimpleFactory = new AuthSimpleFactory($moduleConfig);
132132
$this->services[AuthSimpleFactory::class] = $authSimpleFactory;
133133

134-
$authContextService = new AuthContextService($moduleConfig, $authSimpleFactory);
134+
$sspBridge = new SspBridge();
135+
$this->services[SspBridge::class] = $sspBridge;
136+
137+
$authContextService = new AuthContextService(
138+
$moduleConfig,
139+
$authSimpleFactory,
140+
$sspBridge,
141+
);
135142
$this->services[AuthContextService::class] = $authContextService;
136143

137144
$session = Session::getSessionFromRequest();
138145
$this->services[Session::class] = $session;
139146

140-
$sspBridge = new SspBridge();
141-
$this->services[SspBridge::class] = $sspBridge;
142-
143147
$helpers = new Helpers();
144148
$this->services[Helpers::class] = $helpers;
145149

tests/unit/src/Bridges/SspBridge/UtilsTest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use PHPUnit\Framework\Attributes\CoversClass;
88
use PHPUnit\Framework\TestCase;
99
use SimpleSAML\Module\oidc\Bridges\SspBridge\Utils;
10+
use SimpleSAML\Utils\Attributes;
1011
use SimpleSAML\Utils\Auth;
1112
use SimpleSAML\Utils\Config;
1213
use SimpleSAML\Utils\HTTP;
@@ -44,4 +45,9 @@ public function testCanBuildAuthInstance(): void
4445
{
4546
$this->assertInstanceOf(Auth::class, $this->sut()->auth());
4647
}
48+
49+
public function testCanBuileAttributesInstance(): void
50+
{
51+
$this->assertInstanceOf(Attributes::class, $this->sut()->attributes());
52+
}
4753
}

tests/unit/src/Services/AuthContextServiceTest.php

Lines changed: 41 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@
1010
use SimpleSAML\Auth\Simple;
1111
use SimpleSAML\Configuration;
1212
use SimpleSAML\Error\Exception;
13+
use SimpleSAML\Module\oidc\Bridges\SspBridge;
1314
use SimpleSAML\Module\oidc\Factories\AuthSimpleFactory;
1415
use SimpleSAML\Module\oidc\ModuleConfig;
1516
use SimpleSAML\Module\oidc\Services\AuthContextService;
17+
use SimpleSAML\Utils\Attributes;
1618

1719
/**
1820
* @covers \SimpleSAML\Module\oidc\Services\AuthContextService
@@ -28,6 +30,9 @@ class AuthContextServiceTest extends TestCase
2830
protected MockObject $moduleConfigMock;
2931
protected MockObject $authSimpleService;
3032
protected MockObject $authSimpleFactory;
33+
protected MockObject $sspBridgeMock;
34+
protected MockObject $sspBridgeUtilsMock;
35+
protected MockObject $sspBridgeUtilsAttributesMock;
3136

3237
/**
3338
* @throws \PHPUnit\Framework\MockObject\Exception
@@ -52,21 +57,35 @@ protected function setUp(): void
5257

5358
$this->authSimpleFactory = $this->createMock(AuthSimpleFactory::class);
5459
$this->authSimpleFactory->method('getDefaultAuthSource')->willReturn($this->authSimpleService);
60+
61+
$this->sspBridgeMock = $this->createMock(SspBridge::class);
62+
$this->sspBridgeUtilsMock = $this->createMock(SspBridge\Utils::class);
63+
$this->sspBridgeMock->method('utils')->willReturn($this->sspBridgeUtilsMock);
64+
$this->sspBridgeUtilsAttributesMock = $this->createMock(Attributes::class);
65+
$this->sspBridgeUtilsMock->method('attributes')->willReturn($this->sspBridgeUtilsAttributesMock);
5566
}
5667

57-
protected function prepareMockedInstance(): AuthContextService
58-
{
68+
protected function sut(
69+
?ModuleConfig $moduleConfig = null,
70+
?AuthSimpleFactory $authSimpleFactory = null,
71+
?SspBridge $sspBridge = null,
72+
): AuthContextService {
73+
$moduleConfig ??= $this->moduleConfigMock;
74+
$authSimpleFactory ??= $this->authSimpleFactory;
75+
$sspBridge ??= $this->sspBridgeMock;
76+
5977
return new AuthContextService(
60-
$this->moduleConfigMock,
61-
$this->authSimpleFactory,
78+
$moduleConfig,
79+
$authSimpleFactory,
80+
$sspBridge,
6281
);
6382
}
6483

6584
public function testItIsInitializable(): void
6685
{
6786
$this->assertInstanceOf(
6887
AuthContextService::class,
69-
$this->prepareMockedInstance(),
88+
$this->sut(),
7089
);
7190
}
7291

@@ -77,9 +96,15 @@ public function testItReturnsUsername(): void
7796
{
7897
$this->moduleConfigMock->method('getUserIdentifierAttribute')->willReturn('idAttribute');
7998
$this->authSimpleService->method('getAttributes')->willReturn(self::AUTHORIZED_USER);
99+
$this->sspBridgeUtilsAttributesMock->expects($this->once())->method('getExpectedAttribute')
100+
->with(
101+
self::AUTHORIZED_USER,
102+
'idAttribute',
103+
)
104+
->willReturn(self::AUTHORIZED_USER['idAttribute'][0]);
80105

81106
$this->assertSame(
82-
$this->prepareMockedInstance()->getAuthUserId(),
107+
$this->sut()->getAuthUserId(),
83108
'myUsername',
84109
);
85110
}
@@ -94,8 +119,12 @@ public function testItThrowsWhenNoUsername(): void
94119
->willReturn('attributeNotSet');
95120
$this->authSimpleService->method('getAttributes')->willReturn(self::AUTHORIZED_USER);
96121

122+
$this->sspBridgeUtilsAttributesMock->expects($this->once())->method('getExpectedAttribute')
123+
->with(self::AUTHORIZED_USER)
124+
->willThrowException(new Exception('error'));
125+
97126
$this->expectException(Exception::class);
98-
$this->prepareMockedInstance()->getAuthUserId();
127+
$this->sut()->getAuthUserId();
99128
}
100129

101130
/**
@@ -108,7 +137,7 @@ public function testPermissionsOk(): void
108137
->willReturn($this->permissions);
109138
$this->authSimpleService->method('getAttributes')->willReturn(self::AUTHORIZED_USER);
110139

111-
$this->prepareMockedInstance()->requirePermission('client');
140+
$this->sut()->requirePermission('client');
112141
$this->expectNotToPerformAssertions();
113142
}
114143

@@ -121,7 +150,7 @@ public function testItThrowsIfNotAuthorizedForPermission(): void
121150
->with(ModuleConfig::OPTION_ADMIN_UI_PERMISSIONS, null)
122151
->willReturn($this->permissions);
123152
$this->expectException(RuntimeException::class);
124-
$this->prepareMockedInstance()->requirePermission('no-match');
153+
$this->sut()->requirePermission('no-match');
125154
}
126155

127156
/**
@@ -141,7 +170,7 @@ public function testItThrowsForWrongEntitlements(): void
141170
);
142171

143172
$this->expectException(RuntimeException::class);
144-
$this->prepareMockedInstance()->requirePermission('client');
173+
$this->sut()->requirePermission('client');
145174
}
146175

147176
/**
@@ -160,7 +189,7 @@ public function testItThrowsForNotHavingEntitlementAttribute(): void
160189
);
161190

162191
$this->expectException(RuntimeException::class);
163-
$this->prepareMockedInstance()->requirePermission('client');
192+
$this->sut()->requirePermission('client');
164193
}
165194

166195
/**
@@ -173,6 +202,6 @@ public function testThrowsForNotHavingEnabledPermissions(): void
173202
->willReturn(Configuration::loadFromArray([]));
174203

175204
$this->expectException(RuntimeException::class);
176-
$this->prepareMockedInstance()->requirePermission('client');
205+
$this->sut()->requirePermission('client');
177206
}
178207
}

0 commit comments

Comments
 (0)