Search in sources :

Example 1 with TokenInfo

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;
}
Also used : User(org.stepik.api.objects.users.User) TokenInfo(org.stepik.api.objects.auth.TokenInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with TokenInfo

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;
}
Also used : TokenInfo(org.stepik.api.objects.auth.TokenInfo) StepikClientException(org.stepik.api.exceptions.StepikClientException) NotNull(org.jetbrains.annotations.NotNull)

Example 3 with TokenInfo

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;
}
Also used : StepikApiClient(org.stepik.api.client.StepikApiClient) HttpConfigurable(com.intellij.util.net.HttpConfigurable) HttpTransportClient(org.stepik.api.client.HttpTransportClient) TokenInfo(org.stepik.api.objects.auth.TokenInfo) NotNull(org.jetbrains.annotations.NotNull)

Example 4 with TokenInfo

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;
}
Also used : ClientResponse(org.stepik.api.client.ClientResponse) StepikApiClient(org.stepik.api.client.StepikApiClient) StepikUnauthorizedException(org.stepik.api.exceptions.StepikUnauthorizedException) TransportClient(org.stepik.api.client.TransportClient) HashMap(java.util.HashMap) TokenInfo(org.stepik.api.objects.auth.TokenInfo) StepikClientException(org.stepik.api.exceptions.StepikClientException) NotNull(org.jetbrains.annotations.NotNull)

Example 5 with TokenInfo

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;
}
Also used : ClientResponse(org.stepik.api.client.ClientResponse) StepikApiClient(org.stepik.api.client.StepikApiClient) StepikUnauthorizedException(org.stepik.api.exceptions.StepikUnauthorizedException) TransportClient(org.stepik.api.client.TransportClient) HashMap(java.util.HashMap) TokenInfo(org.stepik.api.objects.auth.TokenInfo) StepikClientException(org.stepik.api.exceptions.StepikClientException) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

NotNull (org.jetbrains.annotations.NotNull)8 TokenInfo (org.stepik.api.objects.auth.TokenInfo)8 StepikApiClient (org.stepik.api.client.StepikApiClient)4 StepikClientException (org.stepik.api.exceptions.StepikClientException)4 CredentialAttributes (com.intellij.credentialStore.CredentialAttributes)2 Credentials (com.intellij.credentialStore.Credentials)2 HttpConfigurable (com.intellij.util.net.HttpConfigurable)2 HashMap (java.util.HashMap)2 ClientResponse (org.stepik.api.client.ClientResponse)2 HttpTransportClient (org.stepik.api.client.HttpTransportClient)2 TransportClient (org.stepik.api.client.TransportClient)2 StepikUnauthorizedException (org.stepik.api.exceptions.StepikUnauthorizedException)2 User (org.stepik.api.objects.users.User)2 PasswordSafe (com.intellij.ide.passwordSafe.PasswordSafe)1 PropertiesComponent (com.intellij.ide.util.PropertiesComponent)1 Application (com.intellij.openapi.application.Application)1 ApplicationManager (com.intellij.openapi.application.ApplicationManager)1 ModalityState (com.intellij.openapi.application.ModalityState)1 Logger (com.intellij.openapi.diagnostic.Logger)1 ProcessCanceledException (com.intellij.openapi.progress.ProcessCanceledException)1