refatoring

This commit is contained in:
yetju000 2019-11-29 21:05:51 +01:00
parent a27bc37e6e
commit b6895a68d6
3 changed files with 13 additions and 12 deletions

View File

@ -47,11 +47,11 @@ public class UsosController {
String oauth_verifier = request.getHeader("oauth_verifier");
try {
String responseToken = authorizationService.authorize(oauth_token, oauth_token_secret, oauth_verifier);
responseHeaders.add("authorization", responseToken);
return ResponseEntity.status(HttpStatus.OK).headers(responseHeaders).body(new TokenResponseDTO(responseToken));
TokenResponseDTO responseToken = authorizationService.authorize(oauth_token, oauth_token_secret, oauth_verifier);
responseHeaders.add("authorization", responseToken.getKey());
return ResponseEntity.status(HttpStatus.OK).headers(responseHeaders).body(responseToken);
} catch (NoSuchAlgorithmException | InvalidKeyException | IOException e) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).headers(responseHeaders).body(new TokenResponseDTO(e.getCause().toString()));
return ResponseEntity.status(HttpStatus.BAD_REQUEST).headers(responseHeaders).body(new TokenResponseDTO(e.getCause().toString(), null));
}
}

View File

@ -3,7 +3,6 @@ package studycave.studycaverestservice.usos;
import io.restassured.response.Response;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.Header;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
@ -11,9 +10,7 @@ import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
@ -58,7 +55,7 @@ public class AuthorizationService {
@Autowired
private UserRepository userRepository;
public String authorize(String oauth_token, String oauth_token_secret, String oauth_verifier) throws IOException, InvalidKeyException, NoSuchAlgorithmException {
public TokenResponseDTO authorize(String oauth_token, String oauth_token_secret, String oauth_verifier) throws IOException, InvalidKeyException, NoSuchAlgorithmException {
List<NameValuePair> queryParams = new ArrayList<>();
queryParams.add(new BasicNameValuePair("oauth_consumer_key", propertiesLoader.getKey()));
queryParams.add(new BasicNameValuePair("oauth_nonce", "" + (int) (Math.random() * 100000000)));
@ -77,7 +74,7 @@ public class AuthorizationService {
oauth_token_secret = tokens[1].replace("oauth_token_secret=", "");
return createUserAccountAndReturnHeaderToken(oauth_token, oauth_token_secret);
}
return "authorization failed";
return new TokenResponseDTO("authorization failed", null);
}
public String requestToken(String oauth_callback) throws UnsupportedEncodingException, InvalidKeyException, NoSuchAlgorithmException {
@ -114,7 +111,7 @@ public class AuthorizationService {
return loggedOut;
}
private String createUserAccountAndReturnHeaderToken(String oauth_token, String oauth_token_secret) throws IOException, InvalidKeyException, NoSuchAlgorithmException {
private TokenResponseDTO createUserAccountAndReturnHeaderToken(String oauth_token, String oauth_token_secret) throws IOException, InvalidKeyException, NoSuchAlgorithmException {
List<NameValuePair> queryParams = new ArrayList<>();
queryParams.add(new BasicNameValuePair("oauth_consumer_key", propertiesLoader.getKey()));
queryParams.add(new BasicNameValuePair("oauth_nonce", "" + (int) (Math.random() * 100000000)));
@ -144,13 +141,15 @@ public class AuthorizationService {
user.setSurname(responseJSON.getString("last_name"));
user.setUsername(id);
user = userRepository.save(user);
return getAuthToken(user.getUsername(), generatedPassword);
String token = getAuthToken(user.getUsername(), generatedPassword);
return new TokenResponseDTO(token, user.getUsername());
}
else {
User user = userCheck.get();
user.getUsosUser().setPassword(bCryptPasswordEncoder.encode(generatedPassword));
user = userRepository.save(user);
return getAuthToken(user.getUsername(), generatedPassword);
String token = getAuthToken(user.getUsername(), generatedPassword);
return new TokenResponseDTO(token, user.getUsername());
}
}

View File

@ -13,4 +13,6 @@ public class TokenResponseDTO {
private String key;
private String username;
}