001package com.box.sdkgen.managers.authorization; 002 003import static com.box.sdkgen.internal.utils.UtilsManager.convertToString; 004import static com.box.sdkgen.internal.utils.UtilsManager.entryOf; 005import static com.box.sdkgen.internal.utils.UtilsManager.mapOf; 006import static com.box.sdkgen.internal.utils.UtilsManager.mergeMaps; 007import static com.box.sdkgen.internal.utils.UtilsManager.prepareParams; 008 009import com.box.sdkgen.networking.auth.Authentication; 010import com.box.sdkgen.networking.fetchoptions.FetchOptions; 011import com.box.sdkgen.networking.fetchoptions.ResponseFormat; 012import com.box.sdkgen.networking.fetchresponse.FetchResponse; 013import com.box.sdkgen.networking.network.NetworkSession; 014import com.box.sdkgen.schemas.accesstoken.AccessToken; 015import com.box.sdkgen.schemas.postoauth2revoke.PostOAuth2Revoke; 016import com.box.sdkgen.schemas.postoauth2token.PostOAuth2Token; 017import com.box.sdkgen.schemas.postoauth2tokenrefreshaccesstoken.PostOAuth2TokenRefreshAccessToken; 018import com.box.sdkgen.serialization.json.JsonManager; 019import java.util.Map; 020 021public class AuthorizationManager { 022 023 public Authentication auth; 024 025 public NetworkSession networkSession; 026 027 public AuthorizationManager() { 028 this.networkSession = new NetworkSession(); 029 } 030 031 protected AuthorizationManager(Builder builder) { 032 this.auth = builder.auth; 033 this.networkSession = builder.networkSession; 034 } 035 036 /** 037 * Authorize a user by sending them through the [Box](https://box.com) website and request their 038 * permission to act on their behalf. 039 * 040 * <p>This is the first step when authenticating a user using OAuth 2.0. To request a user's 041 * authorization to use the Box APIs on their behalf you will need to send a user to the URL with 042 * this format. 043 * 044 * @param queryParams Query parameters of authorizeUser method 045 */ 046 public void authorizeUser(AuthorizeUserQueryParams queryParams) { 047 authorizeUser(queryParams, new AuthorizeUserHeaders()); 048 } 049 050 /** 051 * Authorize a user by sending them through the [Box](https://box.com) website and request their 052 * permission to act on their behalf. 053 * 054 * <p>This is the first step when authenticating a user using OAuth 2.0. To request a user's 055 * authorization to use the Box APIs on their behalf you will need to send a user to the URL with 056 * this format. 057 * 058 * @param queryParams Query parameters of authorizeUser method 059 * @param headers Headers of authorizeUser method 060 */ 061 public void authorizeUser(AuthorizeUserQueryParams queryParams, AuthorizeUserHeaders headers) { 062 Map<String, String> queryParamsMap = 063 prepareParams( 064 mapOf( 065 entryOf("response_type", convertToString(queryParams.getResponseType())), 066 entryOf("client_id", convertToString(queryParams.getClientId())), 067 entryOf("redirect_uri", convertToString(queryParams.getRedirectUri())), 068 entryOf("state", convertToString(queryParams.getState())), 069 entryOf("scope", convertToString(queryParams.getScope())))); 070 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 071 FetchResponse response = 072 this.networkSession 073 .getNetworkClient() 074 .fetch( 075 new FetchOptions.Builder( 076 String.join( 077 "", this.networkSession.getBaseUrls().getOauth2Url(), "/authorize"), 078 "GET") 079 .params(queryParamsMap) 080 .headers(headersMap) 081 .responseFormat(ResponseFormat.NO_CONTENT) 082 .auth(this.auth) 083 .networkSession(this.networkSession) 084 .build()); 085 } 086 087 /** 088 * Request an Access Token using either a client-side obtained OAuth 2.0 authorization code or a 089 * server-side JWT assertion. 090 * 091 * <p>An Access Token is a string that enables Box to verify that a request belongs to an 092 * authorized session. In the normal order of operations you will begin by requesting 093 * authentication from the [authorize](https://developer.box.com/reference/get-authorize) endpoint 094 * and Box will send you an authorization code. 095 * 096 * <p>You will then send this code to this endpoint to exchange it for an Access Token. The 097 * returned Access Token can then be used to to make Box API calls. 098 * 099 * @param requestBody Request body of requestAccessToken method 100 */ 101 public AccessToken requestAccessToken(PostOAuth2Token requestBody) { 102 return requestAccessToken(requestBody, new RequestAccessTokenHeaders()); 103 } 104 105 /** 106 * Request an Access Token using either a client-side obtained OAuth 2.0 authorization code or a 107 * server-side JWT assertion. 108 * 109 * <p>An Access Token is a string that enables Box to verify that a request belongs to an 110 * authorized session. In the normal order of operations you will begin by requesting 111 * authentication from the [authorize](https://developer.box.com/reference/get-authorize) endpoint 112 * and Box will send you an authorization code. 113 * 114 * <p>You will then send this code to this endpoint to exchange it for an Access Token. The 115 * returned Access Token can then be used to to make Box API calls. 116 * 117 * @param requestBody Request body of requestAccessToken method 118 * @param headers Headers of requestAccessToken method 119 */ 120 public AccessToken requestAccessToken( 121 PostOAuth2Token requestBody, RequestAccessTokenHeaders headers) { 122 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 123 FetchResponse response = 124 this.networkSession 125 .getNetworkClient() 126 .fetch( 127 new FetchOptions.Builder( 128 String.join( 129 "", this.networkSession.getBaseUrls().getBaseUrl(), "/oauth2/token"), 130 "POST") 131 .headers(headersMap) 132 .data(JsonManager.serialize(requestBody)) 133 .contentType("application/x-www-form-urlencoded") 134 .responseFormat(ResponseFormat.JSON) 135 .auth(this.auth) 136 .networkSession(this.networkSession) 137 .build()); 138 return JsonManager.deserialize(response.getData(), AccessToken.class); 139 } 140 141 /** 142 * Refresh an Access Token using its client ID, secret, and refresh token. 143 * 144 * @param requestBody Request body of refreshAccessToken method 145 */ 146 public AccessToken refreshAccessToken(PostOAuth2TokenRefreshAccessToken requestBody) { 147 return refreshAccessToken(requestBody, new RefreshAccessTokenHeaders()); 148 } 149 150 /** 151 * Refresh an Access Token using its client ID, secret, and refresh token. 152 * 153 * @param requestBody Request body of refreshAccessToken method 154 * @param headers Headers of refreshAccessToken method 155 */ 156 public AccessToken refreshAccessToken( 157 PostOAuth2TokenRefreshAccessToken requestBody, RefreshAccessTokenHeaders headers) { 158 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 159 FetchResponse response = 160 this.networkSession 161 .getNetworkClient() 162 .fetch( 163 new FetchOptions.Builder( 164 String.join( 165 "", 166 this.networkSession.getBaseUrls().getBaseUrl(), 167 "/oauth2/token#refresh"), 168 "POST") 169 .headers(headersMap) 170 .data(JsonManager.serialize(requestBody)) 171 .contentType("application/x-www-form-urlencoded") 172 .responseFormat(ResponseFormat.JSON) 173 .auth(this.auth) 174 .networkSession(this.networkSession) 175 .build()); 176 return JsonManager.deserialize(response.getData(), AccessToken.class); 177 } 178 179 /** 180 * Revoke an active Access Token, effectively logging a user out that has been previously 181 * authenticated. 182 * 183 * @param requestBody Request body of revokeAccessToken method 184 */ 185 public void revokeAccessToken(PostOAuth2Revoke requestBody) { 186 revokeAccessToken(requestBody, new RevokeAccessTokenHeaders()); 187 } 188 189 /** 190 * Revoke an active Access Token, effectively logging a user out that has been previously 191 * authenticated. 192 * 193 * @param requestBody Request body of revokeAccessToken method 194 * @param headers Headers of revokeAccessToken method 195 */ 196 public void revokeAccessToken(PostOAuth2Revoke requestBody, RevokeAccessTokenHeaders headers) { 197 Map<String, String> headersMap = prepareParams(mergeMaps(mapOf(), headers.getExtraHeaders())); 198 FetchResponse response = 199 this.networkSession 200 .getNetworkClient() 201 .fetch( 202 new FetchOptions.Builder( 203 String.join( 204 "", this.networkSession.getBaseUrls().getBaseUrl(), "/oauth2/revoke"), 205 "POST") 206 .headers(headersMap) 207 .data(JsonManager.serialize(requestBody)) 208 .contentType("application/x-www-form-urlencoded") 209 .responseFormat(ResponseFormat.NO_CONTENT) 210 .auth(this.auth) 211 .networkSession(this.networkSession) 212 .build()); 213 } 214 215 public Authentication getAuth() { 216 return auth; 217 } 218 219 public NetworkSession getNetworkSession() { 220 return networkSession; 221 } 222 223 public static class Builder { 224 225 protected Authentication auth; 226 227 protected NetworkSession networkSession; 228 229 public Builder() {} 230 231 public Builder auth(Authentication auth) { 232 this.auth = auth; 233 return this; 234 } 235 236 public Builder networkSession(NetworkSession networkSession) { 237 this.networkSession = networkSession; 238 return this; 239 } 240 241 public AuthorizationManager build() { 242 if (this.networkSession == null) { 243 this.networkSession = new NetworkSession(); 244 } 245 return new AuthorizationManager(this); 246 } 247 } 248}