88 */
99namespace OCA \OAuth2 \Controller ;
1010
11- use OC \Authentication \Token \IProvider as IAuthTokenProvider ;
12- use OCA \OAuth2 \Db \AccessTokenMapper ;
13- use OCA \OAuth2 \Db \Client ;
14- use OCA \OAuth2 \Db \ClientMapper ;
11+ use OCA \OAuth2 \Service \ClientService ;
1512use OCP \AppFramework \Controller ;
1613use OCP \AppFramework \Http ;
1714use OCP \AppFramework \Http \Attribute \PasswordConfirmationRequired ;
1815use OCP \AppFramework \Http \JSONResponse ;
19- use OCP \Authentication \Exceptions \InvalidTokenException ;
20- use OCP \Authentication \Exceptions \WipeTokenException ;
2116use OCP \IL10N ;
2217use OCP \IRequest ;
23- use OCP \IUser ;
24- use OCP \IUserManager ;
25- use OCP \Security \ICrypto ;
26- use OCP \Security \ISecureRandom ;
27- use Psr \Log \LoggerInterface ;
2818
2919class SettingsController extends Controller {
30-
31- public const validChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 ' ;
32-
3320 public function __construct (
3421 string $ appName ,
3522 IRequest $ request ,
36- private ClientMapper $ clientMapper ,
37- private ISecureRandom $ secureRandom ,
38- private AccessTokenMapper $ accessTokenMapper ,
3923 private IL10N $ l ,
40- private IAuthTokenProvider $ tokenProvider ,
41- private IUserManager $ userManager ,
42- private ICrypto $ crypto ,
43- private LoggerInterface $ logger ,
24+ private readonly ClientService $ clientService ,
4425 ) {
4526 parent ::__construct ($ appName , $ request );
4627 }
@@ -52,55 +33,14 @@ public function addClient(string $name,
5233 return new JSONResponse (['message ' => $ this ->l ->t ('Your redirect URL needs to be a full URL for example: https://yourdomain.com/path ' )], Http::STATUS_BAD_REQUEST );
5334 }
5435
55- $ client = new Client ();
56- $ client ->setName ($ name );
57- $ client ->setRedirectUri ($ redirectUri );
58- $ secret = $ this ->secureRandom ->generate (64 , self ::validChars);
59- $ hashedSecret = bin2hex ($ this ->crypto ->calculateHMAC ($ secret ));
60- $ client ->setSecret ($ hashedSecret );
61- $ client ->setClientIdentifier ($ this ->secureRandom ->generate (64 , self ::validChars));
62- $ client = $ this ->clientMapper ->insert ($ client );
63-
64- $ result = [
65- 'id ' => $ client ->getId (),
66- 'name ' => $ client ->getName (),
67- 'redirectUri ' => $ client ->getRedirectUri (),
68- 'clientId ' => $ client ->getClientIdentifier (),
69- 'clientSecret ' => $ secret ,
70- ];
36+ $ result = $ this ->clientService ->addClient ($ name , $ redirectUri );
7137
7238 return new JSONResponse ($ result );
7339 }
7440
7541 #[PasswordConfirmationRequired]
7642 public function deleteClient (int $ id ): JSONResponse {
77- $ client = $ this ->clientMapper ->getByUid ($ id );
78-
79- $ this ->userManager ->callForSeenUsers (function (IUser $ user ) use ($ client ): void {
80- // Skip tokens that are marked for remote wipe so revoking the
81- // OAuth2 client does not silently cancel a pending wipe.
82- $ tokens = $ this ->tokenProvider ->getTokenByUser ($ user ->getUID ());
83- foreach ($ tokens as $ token ) {
84- if ($ token ->getName () !== $ client ->getName ()) {
85- continue ;
86- }
87- try {
88- $ this ->tokenProvider ->getTokenById ($ token ->getId ());
89- } catch (WipeTokenException $ e ) {
90- $ this ->logger ->info ('Preserving token {tokenId} of user {uid}: marked for remote wipe, OAuth2 client revoke would cancel the wipe. ' , [
91- 'tokenId ' => $ token ->getId (),
92- 'uid ' => $ user ->getUID (),
93- ]);
94- continue ;
95- } catch (InvalidTokenException $ e ) {
96- // Token already invalid; let invalidateTokenById handle it.
97- }
98- $ this ->tokenProvider ->invalidateTokenById ($ user ->getUID (), $ token ->getId ());
99- }
100- });
101-
102- $ this ->accessTokenMapper ->deleteByClientId ($ id );
103- $ this ->clientMapper ->delete ($ client );
43+ $ this ->clientService ->deleteClient ($ id );
10444 return new JSONResponse ([]);
10545 }
10646}
0 commit comments