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