Java Code Examples for com.linecorp.linesdk.LineApiResponse#isSuccess()

The following examples show how to use com.linecorp.linesdk.LineApiResponse#isSuccess() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: LineAuthenticationController.java    From line-sdk-android with Apache License 2.0 6 votes vote down vote up
private void validateIdToken(final LineIdToken idToken, final String userId) {
    final LineApiResponse<OpenIdDiscoveryDocument> response =
            authApiClient.getOpenIdDiscoveryDocument();
    if (!response.isSuccess()) {
        throw new RuntimeException("Failed to get OpenId Discovery Document. "
                                   + " Response Code: " + response.getResponseCode()
                                   + " Error Data: " + response.getErrorData());
    }

    final OpenIdDiscoveryDocument openIdDiscoveryDoc = response.getResponseData();

    final IdTokenValidator idTokenValidator = new Builder()
            .idToken(idToken)
            .expectedIssuer(openIdDiscoveryDoc.getIssuer())
            .expectedUserId(userId)
            .expectedChannelId(config.getChannelId())
            .expectedNonce(authenticationStatus.getOpenIdNonce())
            .build();

    idTokenValidator.validate();
}
 
Example 2
Source File: OpenIdSigningKeyResolver.java    From line-sdk-android with Apache License 2.0 6 votes vote down vote up
private Key resolveSigningKey(final JwsHeader header) {
    final LineApiResponse<JWKSet> response = apiClient.getJWKSet();
    if (!response.isSuccess()) {
        Log.e(TAG, "failed to get LINE JSON Web Key Set [JWK] document.");

        return null;
    }

    final JWKSet jwkSet = response.getResponseData();

    final String keyId = header.getKeyId();
    final JWK jwk = jwkSet.getJWK(keyId);
    if (jwk == null) {
        Log.e(TAG, "failed to find Key by Id: " + keyId);

        return null;
    }

    final String algorithm = header.getAlgorithm();
    final SignatureAlgorithm alg = SignatureAlgorithm.forName(algorithm);
    if (alg.isEllipticCurve()) {
        return generateECPublicKey(jwk);
    }

    throw new SecurityException("Unsupported signature algorithm '" + algorithm + '\'');
}
 
Example 3
Source File: LineAuthenticationApiClient.java    From line-sdk-android with Apache License 2.0 6 votes vote down vote up
@NonNull
public LineApiResponse<JWKSet> getJWKSet() {
    final LineApiResponse<OpenIdDiscoveryDocument> discoveryDocResponse = getOpenIdDiscoveryDocument();

    if (!discoveryDocResponse.isSuccess()) {
        return LineApiResponse.createAsError(discoveryDocResponse.getResponseCode(),
                discoveryDocResponse.getErrorData());
    }

    final OpenIdDiscoveryDocument openIdDiscoveryDoc = discoveryDocResponse.getResponseData();
    final Uri jwksUri = Uri.parse(openIdDiscoveryDoc.getJwksUri());

    final LineApiResponse<JWKSet> jwkSetResponse =
            httpClient.get(jwksUri,
                    emptyMap(),
                    emptyMap(),
                    JWK_SET_PARSER);
    if (!jwkSetResponse.isSuccess()) {
        Log.e(TAG, "getJWKSet failed: " + jwkSetResponse);
    }

    return jwkSetResponse;
}
 
Example 4
Source File: TalkApiClient.java    From line-sdk-android with Apache License 2.0 6 votes vote down vote up
@NonNull
public LineApiResponse<List<SendMessageResponse>> sendMessageToMultipleUsers(
        @NonNull InternalAccessToken accessToken,
        @NonNull List<String> targetUserIds,
        @NonNull List<MessageData> messages,
        boolean isOttUsed) {
    if (isOttUsed) {
        LineApiResponse<String> ottResponse = getOtt(accessToken, targetUserIds);
        if (ottResponse.isSuccess()) {
            return sendMessageToMultipleUsersUsingOtt(accessToken, ottResponse.getResponseData(), messages);
        } else {
            return LineApiResponse.createAsError(
                    ottResponse.getResponseCode(),
                    ottResponse.getErrorData());
        }
    } else {
        return sendMessageToMultipleUsersUsingUserIds(accessToken, targetUserIds, messages);
    }
}
 
Example 5
Source File: PostLoginActivity.java    From line-sdk-starter-android-v2 with MIT License 6 votes vote down vote up
protected void onPostExecute(LineApiResponse<LineAccessToken> response) {

            if (response.isSuccess()) {
                String updatedAccessToken = lineApiClient.getCurrentAccessToken().getResponseData().getAccessToken();

                // Update the view
                TextView accessTokenField = (TextView) findViewById(R.id.accessTokenField);
                accessTokenField.setText(updatedAccessToken);
                Toast.makeText(getApplicationContext(), "Access Token has been refreshed.", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "Could not refresh the access token.", Toast.LENGTH_SHORT).show();
                Log.e(TAG, response.getErrorData().toString());
            }

            unlockScreenOrientation();
        }
 
Example 6
Source File: GetTargetUserTask.java    From line-sdk-android with Apache License 2.0 5 votes vote down vote up
private void getAllFriends() {
    String nextPageToken = "";
    while (nextPageToken != null) {
        LineApiResponse<GetFriendsResponse> response =
                lineApiClient.getFriends(FriendSortField.RELATION, nextPageToken, true);
        if (!response.isSuccess()) {
            publishProgress(Collections.emptyList());
            return;
        }

        GetFriendsResponse getFriendsResponse = response.getResponseData();
        publishProgress(convertFriendsToTargetUsers(getFriendsResponse.getFriends()));
        nextPageToken = getFriendsResponse.getNextPageRequestToken();
    }
}
 
Example 7
Source File: GetTargetUserTask.java    From line-sdk-android with Apache License 2.0 5 votes vote down vote up
private void getAllGroups() {
    String nextPageToken = "";
    while (nextPageToken != null) {
        LineApiResponse<GetGroupsResponse> response =
                lineApiClient.getGroups(nextPageToken, true);
        if (!response.isSuccess()) {
            publishProgress(Collections.emptyList());
            return;
        }

        GetGroupsResponse getGroupsResponse = response.getResponseData();
        publishProgress(convertGroupsToTargetUsers(getGroupsResponse.getGroups()));
        nextPageToken = getGroupsResponse.getNextPageRequestToken();
    }
}
 
Example 8
Source File: SendMessageTask.java    From line-sdk-android with Apache License 2.0 5 votes vote down vote up
@Override
protected void onPostExecute(LineApiResponse<List<SendMessageResponse>> lineApiResponse) {
    if (apiStatusListener != null) {
        if (lineApiResponse.isSuccess()) {
            apiStatusListener.onSuccess();
        } else {
            apiStatusListener.onFailure();
        }
    }
}
 
Example 9
Source File: LineApiClientImpl.java    From line-sdk-android with Apache License 2.0 5 votes vote down vote up
@NonNull
private LineApiResponse<LineCredential> verifyToken(@NonNull InternalAccessToken accessToken) {
    LineApiResponse<AccessTokenVerificationResult> response =
            oauthApiClient.verifyAccessToken(accessToken);
    if (!response.isSuccess()) {
        return LineApiResponse.createAsError(
                response.getResponseCode(), response.getErrorData());
    }
    AccessTokenVerificationResult verificationResult = response.getResponseData();
    long verifiedClientTimeMillis = System.currentTimeMillis();

    try {
        accessTokenCache.saveAccessToken(
                new InternalAccessToken(
                        accessToken.getAccessToken(),
                        verificationResult.getExpiresInMillis(),
                        verifiedClientTimeMillis,
                        accessToken.getRefreshToken()));
    } catch(Exception exception) {
        return LineApiResponse.createAsError(
                LineApiResponseCode.INTERNAL_ERROR,
                new LineApiError("save access token fail:" + exception.getMessage())
        );
    }

    return LineApiResponse.createAsSuccess(
            new LineCredential(
                    new LineAccessToken(
                            accessToken.getAccessToken(),
                            verificationResult.getExpiresInMillis(),
                            verifiedClientTimeMillis),
                    verificationResult.getScopes()));
}
 
Example 10
Source File: LineAuthenticationApiClient.java    From line-sdk-android with Apache License 2.0 5 votes vote down vote up
@NonNull
public LineApiResponse<OpenIdDiscoveryDocument> getOpenIdDiscoveryDocument() {
    final Uri uri = buildUri(openidDiscoveryDocumentUrl);
    final LineApiResponse<OpenIdDiscoveryDocument> response =
            httpClient.get(uri,
                    emptyMap(),
                    emptyMap(),
                    OPEN_ID_DISCOVERY_DOCUMENT_PARSER);

    if (!response.isSuccess()) {
        Log.e(TAG, "getOpenIdDiscoveryDocument failed: " + response);
    }

    return response;
}
 
Example 11
Source File: LineLogin.java    From cordova-line-login-plugin with Apache License 2.0 5 votes vote down vote up
private void verifyAccessToken(CallbackContext callbackContext) {
    LineApiResponse verifyResponse = lineApiClient.verifyToken();
    if (verifyResponse.isSuccess()) {
        callbackContext.success();
    } else {
        this.SDKError(verifyResponse.getResponseCode().toString(), verifyResponse.getErrorData().toString());
    }
}
 
Example 12
Source File: PostLoginActivity.java    From line-sdk-starter-android-v2 with MIT License 5 votes vote down vote up
protected void onPostExecute(LineApiResponse<LineCredential> response) {

            if (response.isSuccess()) {
                StringBuilder toastStringBuilder = new StringBuilder("Access Token is VALID and contains the permissions ");

                for (String temp : response.getResponseData().getPermission()) {
                    toastStringBuilder.append(temp + ", ");
                }
                Toast.makeText(getApplicationContext(), toastStringBuilder.toString(), Toast.LENGTH_SHORT).show();

            } else {
                Toast.makeText(getApplicationContext(), "Access Token is NOT VALID", Toast.LENGTH_SHORT).show();
            }
            unlockScreenOrientation();
        }
 
Example 13
Source File: PostLoginActivity.java    From line-sdk-starter-android-v2 with MIT License 5 votes vote down vote up
protected void onPostExecute(LineApiResponse<LineProfile> apiResponse) {

            if(apiResponse.isSuccess()) {
                ProfileDialogFragment newFragment = new ProfileDialogFragment();
                newFragment.setProfileInfo(apiResponse.getResponseData());
                newFragment.show(getFragmentManager(), null);
                unlockScreenOrientation();
            } else {
                Toast.makeText(getApplicationContext(), "Failed to get profile.", Toast.LENGTH_SHORT).show();
                Log.e(TAG, "Failed to get Profile: " + apiResponse.getErrorData().toString());
            }
        }
 
Example 14
Source File: PostLoginActivity.java    From line-sdk-starter-android-v2 with MIT License 5 votes vote down vote up
protected void onPostExecute(LineApiResponse apiResponse){

            if(apiResponse.isSuccess()){
                Toast.makeText(getApplicationContext(), "Logout Successful", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(getApplicationContext(), "Logout Failed", Toast.LENGTH_SHORT).show();
                Log.e(TAG, "Logout Failed: " + apiResponse.getErrorData().toString());
            }
            unlockScreenOrientation();
        }
 
Example 15
Source File: LineAuthenticationController.java    From line-sdk-android with Apache License 2.0 4 votes vote down vote up
@Override
protected LineLoginResult doInBackground(@Nullable BrowserAuthenticationApi.Result... params) {
    BrowserAuthenticationApi.Result authResult = params[0];
    String requestToken = authResult.getRequestToken();
    PKCECode pkceCode = authenticationStatus.getPKCECode();
    String sentRedirectUri = authenticationStatus.getSentRedirectUri();
    if (TextUtils.isEmpty(requestToken)
            || pkceCode == null
            || TextUtils.isEmpty(sentRedirectUri)) {
        return LineLoginResult.internalError("Requested data is missing.");
    }

    // Acquire access token
    LineApiResponse<IssueAccessTokenResult> accessTokenResponse =
            authApiClient.issueAccessToken(
                    config.getChannelId(), requestToken, pkceCode, sentRedirectUri);
    if (!accessTokenResponse.isSuccess()) {
        return LineLoginResult.error(accessTokenResponse);
    }

    IssueAccessTokenResult issueAccessTokenResult = accessTokenResponse.getResponseData();
    InternalAccessToken accessToken = issueAccessTokenResult.getAccessToken();
    List<Scope> scopes = issueAccessTokenResult.getScopes();

    LineProfile lineProfile = null;
    String userId = null;
    if (scopes.contains(Scope.PROFILE)) {
        // Acquire account information
        LineApiResponse<LineProfile> profileResponse = talkApiClient.getProfile(accessToken);
        if (!profileResponse.isSuccess()) {
            return LineLoginResult.error(profileResponse);
        }
        lineProfile = profileResponse.getResponseData();
        userId = lineProfile.getUserId();
    }

    // Cache the acquired access token
    accessTokenCache.saveAccessToken(accessToken);

    final LineIdToken idToken = issueAccessTokenResult.getIdToken();
    if (idToken != null) {
        try {
            validateIdToken(idToken, userId);
        } catch (final Exception e) {
            return LineLoginResult.internalError(e.getMessage());
        }
    }

    return new LineLoginResult.Builder()
            .nonce(authenticationStatus.getOpenIdNonce())
            .lineProfile(lineProfile)
            .lineIdToken(idToken)
            .friendshipStatusChanged(authResult.getFriendshipStatusChanged())
            .lineCredential(new LineCredential(
                    new LineAccessToken(
                            accessToken.getAccessToken(),
                            accessToken.getExpiresInMillis(),
                            accessToken.getIssuedClientTimeMillis()),
                    scopes
            ))
            .build();
}