Search in sources :

Example 11 with AuthorizeResponse

use of com.amazonaws.mobile.client.internal.oauth2.AuthorizeResponse in project steve by RWTH-i5-IDSG.

the class StressTestSoapOCPP16 method attackInternal.

protected void attackInternal() throws Exception {
    final List<String> idTags = getRandomStrings(ID_TAG_COUNT);
    final List<String> chargeBoxIds = getRandomStrings(CHARGE_BOX_COUNT);
    StressTester.Runnable runnable = new StressTester.Runnable() {

        private final ThreadLocal<String> threadLocalChargeBoxId = new ThreadLocal<>();

        @Override
        public void beforeRepeat() {
            CentralSystemService client = getForOcpp16(path);
            ThreadLocalRandom localRandom = ThreadLocalRandom.current();
            threadLocalChargeBoxId.set(chargeBoxIds.get(localRandom.nextInt(chargeBoxIds.size())));
            String chargeBoxId = threadLocalChargeBoxId.get();
            // to insert threadLocalChargeBoxId into db
            BootNotificationResponse boot = client.bootNotification(new BootNotificationRequest().withChargePointVendor(getRandomString()).withChargePointModel(getRandomString()), chargeBoxId);
            Assertions.assertEquals(RegistrationStatus.ACCEPTED, boot.getStatus());
        }

        @Override
        public void toRepeat() {
            CentralSystemService client = getForOcpp16(path);
            ThreadLocalRandom localRandom = ThreadLocalRandom.current();
            String chargeBoxId = threadLocalChargeBoxId.get();
            String idTag = idTags.get(localRandom.nextInt(idTags.size()));
            int connectorId = localRandom.nextInt(1, CONNECTOR_COUNT_PER_CHARGE_BOX + 1);
            int transactionStart = localRandom.nextInt(0, Integer.MAX_VALUE);
            int transactionStop = localRandom.nextInt(transactionStart + 1, Integer.MAX_VALUE);
            HeartbeatResponse heartbeat = client.heartbeat(new HeartbeatRequest(), chargeBoxId);
            Assertions.assertNotNull(heartbeat);
            for (int i = 0; i <= CONNECTOR_COUNT_PER_CHARGE_BOX; i++) {
                StatusNotificationResponse status = client.statusNotification(new StatusNotificationRequest().withErrorCode(ChargePointErrorCode.NO_ERROR).withStatus(ChargePointStatus.AVAILABLE).withConnectorId(i).withTimestamp(DateTime.now()), chargeBoxId);
                Assertions.assertNotNull(status);
            }
            AuthorizeResponse auth = client.authorize(new AuthorizeRequest().withIdTag(idTag), chargeBoxId);
            Assertions.assertNotEquals(AuthorizationStatus.ACCEPTED, auth.getIdTagInfo().getStatus());
            StartTransactionResponse start = client.startTransaction(new StartTransactionRequest().withConnectorId(connectorId).withIdTag(idTag).withTimestamp(DateTime.now()).withMeterStart(transactionStart), chargeBoxId);
            Assertions.assertNotNull(start);
            StatusNotificationResponse statusStart = client.statusNotification(new StatusNotificationRequest().withErrorCode(ChargePointErrorCode.NO_ERROR).withStatus(ChargePointStatus.CHARGING).withConnectorId(connectorId).withTimestamp(DateTime.now()), chargeBoxId);
            Assertions.assertNotNull(statusStart);
            MeterValuesResponse meter = client.meterValues(new MeterValuesRequest().withConnectorId(connectorId).withTransactionId(start.getTransactionId()).withMeterValue(getMeterValues(transactionStart, transactionStop)), chargeBoxId);
            Assertions.assertNotNull(meter);
            StopTransactionResponse stop = client.stopTransaction(new StopTransactionRequest().withTransactionId(start.getTransactionId()).withTimestamp(DateTime.now()).withIdTag(idTag).withMeterStop(transactionStop), chargeBoxId);
            Assertions.assertNotNull(stop);
            StatusNotificationResponse statusStop = client.statusNotification(new StatusNotificationRequest().withErrorCode(ChargePointErrorCode.NO_ERROR).withStatus(ChargePointStatus.AVAILABLE).withConnectorId(connectorId).withTimestamp(DateTime.now()), chargeBoxId);
            Assertions.assertNotNull(statusStop);
        }

        @Override
        public void afterRepeat() {
        }
    };
    StressTester tester = new StressTester(THREAD_COUNT, REPEAT_COUNT_PER_THREAD);
    tester.test(runnable);
    tester.shutDown();
}
Also used : HeartbeatResponse(ocpp.cs._2015._10.HeartbeatResponse) MeterValuesResponse(ocpp.cs._2015._10.MeterValuesResponse) StopTransactionResponse(ocpp.cs._2015._10.StopTransactionResponse) AuthorizeRequest(ocpp.cs._2015._10.AuthorizeRequest) MeterValuesRequest(ocpp.cs._2015._10.MeterValuesRequest) CentralSystemService(ocpp.cs._2015._10.CentralSystemService) StatusNotificationResponse(ocpp.cs._2015._10.StatusNotificationResponse) BootNotificationRequest(ocpp.cs._2015._10.BootNotificationRequest) Helpers.getRandomString(de.rwth.idsg.steve.utils.Helpers.getRandomString) StressTester(de.rwth.idsg.steve.utils.StressTester) HeartbeatRequest(ocpp.cs._2015._10.HeartbeatRequest) BootNotificationResponse(ocpp.cs._2015._10.BootNotificationResponse) AuthorizeResponse(ocpp.cs._2015._10.AuthorizeResponse) StopTransactionRequest(ocpp.cs._2015._10.StopTransactionRequest) StartTransactionRequest(ocpp.cs._2015._10.StartTransactionRequest) StartTransactionResponse(ocpp.cs._2015._10.StartTransactionResponse) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) StatusNotificationRequest(ocpp.cs._2015._10.StatusNotificationRequest)

Example 12 with AuthorizeResponse

use of com.amazonaws.mobile.client.internal.oauth2.AuthorizeResponse in project steve by RWTH-i5-IDSG.

the class Issue73Fix method sendAuth.

private static void sendAuth(CentralSystemService client, String chargeBoxId, AuthorizationStatus expected) {
    AuthorizeResponse auth = client.authorize(new AuthorizeRequest().withIdTag(REGISTERED_OCPP_TAG), chargeBoxId);
    Assertions.assertNotNull(auth);
    Assertions.assertEquals(expected, auth.getIdTagInfo().getStatus());
}
Also used : AuthorizeResponse(ocpp.cs._2015._10.AuthorizeResponse) AuthorizeRequest(ocpp.cs._2015._10.AuthorizeRequest)

Example 13 with AuthorizeResponse

use of com.amazonaws.mobile.client.internal.oauth2.AuthorizeResponse in project aws-sdk-android by aws-amplify.

the class OAuth2Utils method _showSignInOAuth2UI.

private Runnable _showSignInOAuth2UI(final Activity callingActivity, final SignInUIOptions signInUIOptions, final Callback<UserStateDetails> callback) {
    return new Runnable() {

        @Override
        public void run() {
            final HostedUIOptions hostedUIOptions = signInUIOptions.getHostedUIOptions();
            // Reset settings to JSON
            JSONObject hostedUIJSON = getHostedUIJSONFromJSON();
            if (hostedUIJSON == null) {
                callback.onError(new Exception("Could not create OAuth configuration object"));
            }
            if (hostedUIOptions.getFederationEnabled() != null) {
                mStore.set(FEDERATION_ENABLED_KEY, hostedUIOptions.getFederationEnabled() ? "true" : "false");
            } else {
                mStore.set(FEDERATION_ENABLED_KEY, "true");
            }
            mStore.set(SIGN_IN_MODE, SignInMode.OAUTH2.toString());
            if (isFederationEnabled() && hostedUIOptions.getFederationProviderName() == null) {
                throw new IllegalArgumentException("OAuth flow requires a federation provider name if federation is enabled.");
            }
            if (hostedUIOptions.getSignOutQueryParameters() != null) {
                try {
                    JSONObject signOutParams = new JSONObject();
                    for (Map.Entry<String, String> e : hostedUIOptions.getSignOutQueryParameters().entrySet()) {
                        signOutParams.put(e.getKey(), e.getValue());
                    }
                    hostedUIJSON.put("SignOutQueryParameters", signOutParams);
                } catch (JSONException e1) {
                    callback.onError(new Exception("Failed to construct sign-out query parameters", e1));
                    return;
                }
            }
            if (hostedUIOptions.getTokenQueryParameters() != null) {
                try {
                    JSONObject tokenParams = new JSONObject();
                    for (Map.Entry<String, String> e : hostedUIOptions.getTokenQueryParameters().entrySet()) {
                        tokenParams.put(e.getKey(), e.getValue());
                    }
                    hostedUIJSON.put("TokenQueryParameters", tokenParams);
                } catch (JSONException e1) {
                    callback.onError(new Exception("Failed to construct token query parameters", e1));
                    return;
                }
            }
            mStore.set(HOSTED_UI_KEY, hostedUIJSON.toString());
            Uri.Builder authorizeUriBuilder;
            try {
                authorizeUriBuilder = Uri.parse(hostedUIJSON.getString("SignInURI")).buildUpon();
                if (hostedUIOptions.getSignInQueryParameters() != null) {
                    for (Map.Entry<String, String> e : hostedUIOptions.getSignInQueryParameters().entrySet()) {
                        authorizeUriBuilder.appendQueryParameter(e.getKey(), e.getValue());
                    }
                }
                authorizeUriBuilder.appendQueryParameter("redirect_uri", hostedUIJSON.getString("SignInRedirectURI"));
                authorizeUriBuilder.appendQueryParameter("scopes", hostedUIJSON.getJSONArray("Scopes").join(" "));
                authorizeUriBuilder.appendQueryParameter("client_id", hostedUIJSON.getString("AppClientId"));
            } catch (Exception e) {
                throw new RuntimeException("Failed to construct authorization url for OAuth", e);
            }
            Uri.Builder tokensUriBuilder;
            final Map<String, String> tokensBody = new HashMap<String, String>();
            try {
                tokensUriBuilder = Uri.parse(hostedUIJSON.getString("TokenURI")).buildUpon();
                if (hostedUIOptions.getTokenQueryParameters() != null) {
                    for (Map.Entry<String, String> e : hostedUIOptions.getTokenQueryParameters().entrySet()) {
                        tokensUriBuilder.appendQueryParameter(e.getKey(), e.getValue());
                    }
                }
                tokensBody.put("client_id", hostedUIJSON.getString("AppClientId"));
                tokensBody.put("redirect_uri", hostedUIJSON.getString("SignInRedirectURI"));
            } catch (Exception e) {
                throw new RuntimeException("Failed to construct tokens url for OAuth", e);
            }
            final Uri tokensUri = tokensUriBuilder.build();
            mOAuth2Client.authorize(authorizeUriBuilder.build(), new Callback<AuthorizeResponse>() {

                @Override
                public void onResult(AuthorizeResponse result) {
                    Log.i(TAG, "onResult: OAuth2 callback occurred, exchanging code for token");
                    mOAuth2Client.requestTokens(tokensUri, new HashMap<String, String>(), tokensBody, result.getCode(), new Callback<OAuth2Tokens>() {

                        @Override
                        public void onResult(OAuth2Tokens result) {
                            if (isFederationEnabled()) {
                                federatedSignInWithoutAssigningState(hostedUIOptions.getFederationProviderName(), // TODO verify id token is correct, this would mean OAuth support requires scope openid
                                result.getIdToken(), new Callback<UserStateDetails>() {

                                    @Override
                                    public void onResult(UserStateDetails result) {
                                        final UserStateDetails userStateDetails = getUserStateDetails(false);
                                        callback.onResult(userStateDetails);
                                        setUserState(userStateDetails);
                                    }

                                    @Override
                                    public void onError(Exception e) {
                                        final UserStateDetails userStateDetails = getUserStateDetails(false);
                                        callback.onResult(userStateDetails);
                                        setUserState(userStateDetails);
                                    }
                                });
                            } else {
                                final UserStateDetails userStateDetails = getUserStateDetails(false);
                                callback.onResult(userStateDetails);
                                setUserState(userStateDetails);
                            }
                        }

                        @Override
                        public void onError(Exception e) {
                            callback.onError(e);
                        }
                    });
                }

                @Override
                public void onError(Exception e) {
                    callback.onError(e);
                }
            });
        }
    };
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) JSONException(org.json.JSONException) Uri(android.net.Uri) JSONException(org.json.JSONException) InvalidUserPoolConfigurationException(com.amazonaws.services.cognitoidentityprovider.model.InvalidUserPoolConfigurationException) AmazonClientException(com.amazonaws.AmazonClientException) NotAuthorizedException(com.amazonaws.services.cognitoidentity.model.NotAuthorizedException) AuthorizeResponse(com.amazonaws.mobile.client.internal.oauth2.AuthorizeResponse) CustomTabsCallback(androidx.browser.customtabs.CustomTabsCallback) InternalCallback(com.amazonaws.mobile.client.internal.InternalCallback) JSONObject(org.json.JSONObject) ReturningRunnable(com.amazonaws.mobile.client.internal.ReturningRunnable) OAuth2Tokens(com.amazonaws.mobile.client.internal.oauth2.OAuth2Tokens) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap)

Example 14 with AuthorizeResponse

use of com.amazonaws.mobile.client.internal.oauth2.AuthorizeResponse in project EVSUPERVISION by EnergyTIC.

the class OperationalTestSoapOCPP16 method testUnregisteredIdTag.

@Test
public void testUnregisteredIdTag() {
    CentralSystemService client = getForOcpp16(path);
    AuthorizeResponse auth = client.authorize(new AuthorizeRequest().withIdTag(getRandomString()), REGISTERED_CHARGE_BOX_ID);
    Assertions.assertNotNull(auth);
    Assertions.assertEquals(AuthorizationStatus.INVALID, auth.getIdTagInfo().getStatus());
}
Also used : AuthorizeResponse(ocpp.cs._2015._10.AuthorizeResponse) AuthorizeRequest(ocpp.cs._2015._10.AuthorizeRequest) CentralSystemService(ocpp.cs._2015._10.CentralSystemService) Test(org.junit.jupiter.api.Test)

Aggregations

AuthorizeResponse (ocpp.cs._2015._10.AuthorizeResponse)11 AuthorizeRequest (ocpp.cs._2015._10.AuthorizeRequest)10 CentralSystemService (ocpp.cs._2015._10.CentralSystemService)8 Test (org.junit.jupiter.api.Test)6 HeartbeatRequest (ocpp.cs._2015._10.HeartbeatRequest)4 HeartbeatResponse (ocpp.cs._2015._10.HeartbeatResponse)4 MeterValuesRequest (ocpp.cs._2015._10.MeterValuesRequest)4 MeterValuesResponse (ocpp.cs._2015._10.MeterValuesResponse)4 StartTransactionRequest (ocpp.cs._2015._10.StartTransactionRequest)4 StartTransactionResponse (ocpp.cs._2015._10.StartTransactionResponse)4 StatusNotificationRequest (ocpp.cs._2015._10.StatusNotificationRequest)4 StatusNotificationResponse (ocpp.cs._2015._10.StatusNotificationResponse)4 StopTransactionRequest (ocpp.cs._2015._10.StopTransactionRequest)4 StopTransactionResponse (ocpp.cs._2015._10.StopTransactionResponse)4 ChargePoint (de.rwth.idsg.steve.repository.dto.ChargePoint)2 Helpers.getRandomString (de.rwth.idsg.steve.utils.Helpers.getRandomString)2 StressTester (de.rwth.idsg.steve.utils.StressTester)2 List (java.util.List)2 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)2 TransactionRecord (jooq.steve.db.tables.records.TransactionRecord)2