1515use OCA \Talk \Service \HostedSignalingServerService ;
1616use OCP \AppFramework \Http ;
1717use OCP \AppFramework \Http \Attribute \ApiRoute ;
18+ use OCP \AppFramework \Http \Attribute \BruteForceProtection ;
1819use OCP \AppFramework \Http \Attribute \OpenAPI ;
1920use OCP \AppFramework \Http \Attribute \PublicPage ;
21+ use OCP \AppFramework \Http \Attribute \RequestHeader ;
2022use OCP \AppFramework \Http \DataResponse ;
2123use OCP \AppFramework \OCSController ;
2224use OCP \Http \Client \IClientService ;
@@ -42,28 +44,44 @@ public function __construct(
4244 /**
4345 * Get the authentication credentials
4446 *
45- * @return DataResponse<Http::STATUS_OK, array{nonce: string}, array{}>|DataResponse<Http::STATUS_PRECONDITION_FAILED, null, array{}>
47+ * @return DataResponse<Http::STATUS_OK, array{nonce: string}, array{}>|DataResponse<Http::STATUS_FORBIDDEN|Http:: STATUS_PRECONDITION_FAILED, null, array{}>
4648 *
4749 * 200: Authentication credentials returned
50+ * 403: Provided nonce is wrong
4851 * 412: Getting authentication credentials is not possible
4952 */
5053 #[OpenAPI(scope: OpenAPI::SCOPE_IGNORE )]
5154 #[PublicPage]
55+ #[BruteForceProtection(action: 'hosted-hpb-nonce ' )]
56+ #[RequestHeader(name: 'x-account-service-nonce ' , description: 'Random string provided to the hostedsignalingserver entity, so it can verify that it was requested ' )]
5257 #[ApiRoute(verb: 'POST ' , url: '/api/{apiVersion}/hostedsignalingserver/auth ' , requirements: [
5358 'apiVersion ' => '(v1) ' ,
5459 ])]
5560 public function auth (): DataResponse {
61+ $ sentNonce = $ this ->request ->getHeader ('x-account-service-nonce ' );
62+ if ($ sentNonce === '' ) {
63+ $ response = new DataResponse (null , Http::STATUS_FORBIDDEN );
64+ $ response ->throttle ();
65+ return $ response ;
66+ }
67+
5668 $ storedNonce = $ this ->config ->getAppValue ('spreed ' , 'hosted-signaling-server-nonce ' , '' );
57- // reset nonce after one request
58- $ this ->config ->deleteAppValue ('spreed ' , 'hosted-signaling-server-nonce ' );
69+ if ($ storedNonce === '' ) {
70+ return new DataResponse (null , Http::STATUS_PRECONDITION_FAILED );
71+ }
5972
60- if ($ storedNonce !== '' ) {
61- return new DataResponse ([
62- ' nonce ' => $ storedNonce ,
63- ]) ;
73+ if (! hash_equals ( $ storedNonce, $ sentNonce ) ) {
74+ $ response = new DataResponse (null , Http:: STATUS_FORBIDDEN );
75+ $ response -> throttle ();
76+ return $ response ;
6477 }
6578
66- return new DataResponse (null , Http::STATUS_PRECONDITION_FAILED );
79+ // reset nonce after one request
80+ $ this ->config ->deleteAppValue ('spreed ' , 'hosted-signaling-server-nonce ' );
81+
82+ return new DataResponse ([
83+ 'nonce ' => $ storedNonce ,
84+ ]);
6785 }
6886
6987 /**
0 commit comments