99use  OCA \Github \AppInfo \Application ;
1010use  OCA \Github \Reference \GithubIssuePrReferenceProvider ;
1111use  OCA \Github \Service \GithubAPIService ;
12+ use  OCA \Github \Service \SecretService ;
1213use  OCP \AppFramework \Controller ;
14+ use  OCP \AppFramework \Http \Attribute \NoAdminRequired ;
15+ use  OCP \AppFramework \Http \Attribute \NoCSRFRequired ;
1316use  OCP \AppFramework \Http \DataResponse ;
1417use  OCP \AppFramework \Http \RedirectResponse ;
1518use  OCP \AppFramework \Http \TemplateResponse ;
2427class  ConfigController extends  Controller {
2528
2629	public  function  __construct (
27- 		string                                  $ appName
28- 		IRequest $ request
29- 		private  IConfig $ config
30- 		private  IURLGenerator $ urlGenerator
31- 		private  IL10N $ l
32- 		private  IInitialState $ initialStateService
33- 		private  GithubAPIService $ githubAPIService
30+ 		string  $ appName
31+ 		IRequest $ request
32+ 		private  IConfig $ config
33+ 		private  IURLGenerator $ urlGenerator
34+ 		private  IL10N $ l
35+ 		private  IInitialState $ initialStateService
36+ 		private  GithubAPIService $ githubAPIService
37+ 		private  SecretService $ secretService
3438		private  GithubIssuePrReferenceProvider $ githubIssuePrReferenceProvider
35- 		private  ?string                          $ userId
39+ 		private  ?string  $ userId
3640	) {
3741		parent ::__construct ($ appName$ request
3842	}
3943
4044	/** 
41- 	 * @NoAdminRequired 
4245	 * Set config values 
4346	 * 
4447	 * @param array $values key/value pairs to store in user preferences 
4548	 * @return DataResponse 
4649	 * @throws PreConditionNotMetException 
4750	 */ 
51+ 	#[NoAdminRequired]
4852	public  function  setConfig (array  $ valuesDataResponse 
4953		// revoke the oauth token if needed 
5054		if  (isset ($ values'token ' ]) && $ values'token ' ] === '' ) {
@@ -56,7 +60,11 @@ public function setConfig(array $values): DataResponse {
5660
5761		// save values 
5862		foreach  ($ valuesas  $ key$ value
59- 			$ this config ->setUserValue ($ this userId , Application::APP_ID , $ key$ value
63+ 			if  ($ key'token ' ) {
64+ 				$ this secretService ->setEncryptedUserValue ($ this userId , $ key$ value
65+ 			} else  {
66+ 				$ this config ->setUserValue ($ this userId , Application::APP_ID , $ key$ value
67+ 			}
6068		}
6169		$ result
6270
@@ -76,6 +84,7 @@ public function setConfig(array $values): DataResponse {
7684				$ this config ->deleteUserValue ($ this userId , Application::APP_ID , 'user_name ' );
7785				$ this config ->deleteUserValue ($ this userId , Application::APP_ID , 'user_displayname ' );
7886				$ this config ->deleteUserValue ($ this userId , Application::APP_ID , 'token_type ' );
87+ 				$ this config ->deleteUserValue ($ this userId , Application::APP_ID , 'token ' );
7988				$ result'user_name ' ] = '' ;
8089			}
8190			// connect or disconnect: invalidate the user-related cache 
@@ -92,39 +101,41 @@ public function setConfig(array $values): DataResponse {
92101	 */ 
93102	public  function  setAdminConfig (array  $ valuesDataResponse 
94103		foreach  ($ valuesas  $ key$ value
95- 			$ this config ->setAppValue (Application::APP_ID , $ key$ value
104+ 			if  (in_array ($ key'client_id ' , 'client_secret ' , 'default_link_token ' ], true )) {
105+ 				$ this secretService ->setEncryptedAppValue ($ key$ value
106+ 			} else  {
107+ 				$ this config ->setAppValue (Application::APP_ID , $ key$ value
108+ 			}
96109		}
97110		return  new  DataResponse (1 );
98111	}
99112
100113	/** 
101- 	 * @NoAdminRequired 
102- 	 * @NoCSRFRequired 
103- 	 * 
104114	 * @param string $user_name 
105115	 * @param string $user_displayname 
106116	 * @return TemplateResponse 
107117	 */ 
118+ 	#[NoAdminRequired]
119+ 	#[NoCSRFRequired]
108120	public  function  popupSuccessPage (string  $ user_namestring  $ user_displaynameTemplateResponse 
109121		$ this initialStateService ->provideInitialState ('popup-data ' , ['user_name '  => $ user_name'user_displayname '  => $ user_displayname
110122		return  new  TemplateResponse (Application::APP_ID , 'popupSuccess ' , [], TemplateResponse::RENDER_AS_GUEST );
111123	}
112124
113125	/** 
114- 	 * @NoAdminRequired 
115- 	 * @NoCSRFRequired 
116- 	 * 
117126	 * Receive oauth code and get oauth access token 
118127	 * 
119128	 * @param string $code request code to use when requesting oauth token 
120129	 * @param string $state value that was sent with original GET request. Used to check auth redirection is valid 
121130	 * @return RedirectResponse to user settings 
122131	 * @throws PreConditionNotMetException 
123132	 */ 
133+ 	#[NoAdminRequired]
134+ 	#[NoCSRFRequired]
124135	public  function  oauthRedirect (string  $ codestring  $ stateRedirectResponse 
125136		$ configState$ this config ->getUserValue ($ this userId , Application::APP_ID , 'oauth_state ' );
126- 		$ clientID$ this config -> getAppValue (Application:: APP_ID ,  'client_id ' );
127- 		$ clientSecret$ this config -> getAppValue (Application:: APP_ID ,  'client_secret ' );
137+ 		$ clientID$ this secretService -> getEncryptedAppValue ( 'client_id ' );
138+ 		$ clientSecret$ this secretService -> getEncryptedAppValue ( 'client_secret ' );
128139
129140		// anyway, reset state 
130141		$ this config ->deleteUserValue ($ this userId , Application::APP_ID , 'oauth_state ' );
@@ -139,7 +150,7 @@ public function oauthRedirect(string $code, string $state): RedirectResponse {
139150			if  (isset ($ result'access_token ' ])) {
140151				$ this githubIssuePrReferenceProvider ->invalidateUserCache ($ this userId );
141152				$ accessToken$ result'access_token ' ];
142- 				$ this config -> setUserValue ($ this userId , Application:: APP_ID , 'token ' , $ accessToken
153+ 				$ this secretService -> setEncryptedUserValue ($ this userId , 'token ' , $ accessToken
143154				$ this config ->setUserValue ($ this userId , Application::APP_ID , 'token_type ' , 'oauth ' );
144155				$ userInfo$ this storeUserInfo ();
145156
0 commit comments