use of org.stepik.api.objects.auth.TokenInfo in project intellij-plugins by StepicOrg.
the class StepikAuthManager method showDialog.
@NotNull
private static StepikAuthState showDialog() {
Map<String, String> map = AuthDialog.showAuthForm();
StepikAuthState newState = NOT_AUTH;
TokenInfo tokenInfo = new TokenInfo();
if (!map.isEmpty() && !map.containsKey("error")) {
newState = AUTH;
tokenInfo.setAccessToken(map.get("access_token"));
tokenInfo.setExpiresIn(Integer.valueOf(map.getOrDefault("expires_in", "0")));
tokenInfo.setScope(map.get("scope"));
tokenInfo.setTokenType(map.get("token_type"));
tokenInfo.setRefreshToken(map.get("refresh_token"));
}
stepikApiClient.setTokenInfo(tokenInfo);
if (newState == AUTH && tokenInfo.getAccessToken() != null) {
User user = getCurrentUser(true);
if (!user.isGuest()) {
setTokenInfo(user.getId(), tokenInfo);
} else {
newState = NOT_AUTH;
}
}
logger.info("Show the authentication dialog with result: " + newState);
return newState;
}
use of org.stepik.api.objects.auth.TokenInfo in project intellij-plugins by StepicOrg.
the class StepikAuthManager method minorLogin.
@NotNull
private static StepikAuthState minorLogin() {
if (isAuthenticated()) {
return AUTH;
}
String refreshToken = stepikApiClient.getTokenInfo().getRefreshToken();
if (refreshToken == null) {
TokenInfo tokenInfo = getTokenInfo(getLastUser());
refreshToken = tokenInfo.getRefreshToken();
}
if (refreshToken != null) {
try {
stepikApiClient.oauth2().userAuthenticationRefresh(CLIENT_ID, refreshToken).execute();
logger.info("Refresh a token is successfully");
return AUTH;
} catch (StepikClientException re) {
logger.info("Refresh a token failed: " + re.getMessage());
}
}
return NOT_AUTH;
}
use of org.stepik.api.objects.auth.TokenInfo in project intellij-plugins by StepicOrg.
the class StepikAuthManager method initStepikApiClient.
@NotNull
private static synchronized StepikApiClient initStepikApiClient() {
String osName = System.getProperty("os.name");
String jre = System.getProperty("java.version");
String userAgent = String.format("Stepik Union/%s (%s) StepikApiClient/%s %s/%s JRE/%s", getVersion(), osName, StepikApiClient.getVersion(), getCurrentProduct(), getCurrentProductVersion(), jre);
logger.info(userAgent);
HttpConfigurable instance = HttpConfigurable.getInstance();
StepikApiClient client;
if (instance.USE_HTTP_PROXY) {
logger.info(String.format("Uses proxy: Host = %s, Port = %s", instance.PROXY_HOST, instance.PROXY_PORT));
HttpTransportClient transportClient;
transportClient = HttpTransportClient.getInstance(instance.PROXY_HOST, instance.PROXY_PORT, userAgent);
client = new StepikApiClient(transportClient);
} else {
client = new StepikApiClient(userAgent);
}
long lastUserId = getLastUser();
TokenInfo tokenInfo = getTokenInfo(lastUserId, client);
client.setTokenInfo(tokenInfo);
return client;
}
use of org.stepik.api.objects.auth.TokenInfo in project intellij-plugins by StepicOrg.
the class StepikFilesAction method get.
@NotNull
public String get(@NotNull String url, String contentType) {
StepikApiClient stepikApi = getStepikApiClient();
TransportClient transportClient = stepikApi.getTransportClient();
Map<String, String> headers = new HashMap<>();
TokenInfo tokenInfo = stepikApi.getTokenInfo();
String accessToken = tokenInfo.getAccessToken();
if (accessToken != null) {
String tokenType = tokenInfo.getTokenType();
headers.put(HttpHeaders.AUTHORIZATION, tokenType + " " + accessToken);
}
headers.put(HttpHeaders.CONTENT_TYPE, contentType);
ClientResponse response = transportClient.get(stepikApi, url, headers);
if (response.getStatusCode() / 100 != 2) {
String message = "Failed query to " + url + " returned the status code " + response.getStatusCode();
logger.warn(message);
if (response.getStatusCode() == StatusCodes.SC_UNAUTHORIZED) {
throw new StepikUnauthorizedException(message);
} else {
throw new StepikClientException(message);
}
}
String result = response.getBody();
if (result == null) {
throw new StepikClientException("Request successfully but the response body is null: " + url);
}
return result;
}
use of org.stepik.api.objects.auth.TokenInfo in project intellij-plugins by StepicOrg.
the class StepikAbstractQuery method execute.
@NotNull
public T execute() {
StepikApiClient stepikApi = stepikAction.getStepikApiClient();
TransportClient transportClient = stepikApi.getTransportClient();
String url = getUrl();
Map<String, String> headers = new HashMap<>();
TokenInfo tokenInfo = stepikApi.getTokenInfo();
String accessToken = tokenInfo.getAccessToken();
if (accessToken != null) {
String tokenType = tokenInfo.getTokenType();
headers.put(HttpHeaders.AUTHORIZATION, tokenType + " " + accessToken);
}
headers.put(HttpHeaders.CONTENT_TYPE, getContentType());
ClientResponse response = null;
switch(method) {
case GET:
url += "?" + mapToGetString();
response = transportClient.get(stepikApi, url, headers);
break;
case POST:
response = transportClient.post(stepikApi, url, getBody(), headers);
break;
}
if (response.getStatusCode() / 100 != 2) {
String message = "Failed query to " + getUrl() + " returned the status code " + response.getStatusCode();
logger.warn(message);
if (response.getStatusCode() == StatusCodes.SC_UNAUTHORIZED) {
throw new StepikUnauthorizedException(message);
} else {
throw new StepikClientException(message);
}
}
T result = response.getBody(responseClass);
if (responseClass == VoidResult.class) {
//noinspection unchecked
return (T) new VoidResult();
}
if (result == null) {
throw new StepikClientException("Request successfully but the response body is null: " + url);
}
return result;
}
Aggregations