Skip to content

Improve sms config#350

Open
Thisara-Welmilla wants to merge 5 commits intowso2-extensions:masterfrom
Thisara-Welmilla:improve-sms-config
Open

Improve sms config#350
Thisara-Welmilla wants to merge 5 commits intowso2-extensions:masterfrom
Thisara-Welmilla:improve-sms-config

Conversation

@Thisara-Welmilla
Copy link
Contributor

Proposed changes in this pull request

[List all changes you want to add here. If you fixed an issue, please
add a reference to that issue as well.]

When should this PR be merged

[Please describe any preconditions that need to be addressed before we
can merge this pull request.]

Follow up actions

[List any possible follow-up actions here; for instance, testing data
migrations, software that we need to install on staging and production
environments.]

Developer Checklist (Mandatory)

  • Complete the Developer Checklist in the related product-is issue to track any behavioral change or migration impact.

Checklist (for reviewing)

General

  • Is this PR explained thoroughly? All code changes must be accounted for in the PR description.
  • Is the PR labeled correctly?

Functionality

  • Are all requirements met? Compare implemented functionality with the requirements specification.
  • Does the UI work as expected? There should be no Javascript errors in the console; all resources should load. There should be no unexpected errors. Deliberately try to break the feature to find out if there are corner cases that are not handled.

Code

  • Do you fully understand the introduced changes to the code? If not ask for clarification, it might uncover ways to solve a problem in a more elegant and efficient way.
  • Does the PR introduce any inefficient database requests? Use the debug server to check for duplicate requests.
  • Are all necessary strings marked for translation? All strings that are exposed to users via the UI must be marked for translation.

Tests

  • Are there sufficient test cases? Ensure that all components are tested individually; models, forms, and serializers should be tested in isolation even if a test for a view covers these components.
  • If this is a bug fix, are tests for the issue in place? There must be a test case for the bug to ensure the issue won’t regress. Make sure that the tests break without the new code to fix the issue.
  • If this is a new feature or a significant change to an existing feature? has the manual testing spreadsheet been updated with instructions for manual testing?

Security

  • Confirm this PR doesn't commit any keys, passwords, tokens, usernames, or other secrets.
  • Are all UI and API inputs run through forms or serializers?
  • Are all external inputs validated and sanitized appropriately?
  • Does all branching logic have a default case?
  • Does this solution handle outliers and edge cases gracefully?
  • Are all external communications secured and restricted to SSL?

Documentation

  • Are changes to the UI documented in the platform docs? If this PR introduces new platform site functionality or changes existing ones, the changes should be documented.
  • Are changes to the API documented in the API docs? If this PR introduces new API functionality or changes existing ones, the changes must be documented.
  • Are reusable components documented? If this PR introduces components that are relevant to other developers (for instance a mixin for a view or a generic form) they should be documented in the Wiki.

Comment on lines +117 to +138

public Authentication build() {

switch (authType) {
case BASIC:
resolvedAuthProperties.put(USERNAME_AUTH_PROP, getProperty(propertiesMap, USERNAME_AUTH_PROP));
resolvedAuthProperties.put(PASSWORD_AUTH_PROP, getProperty(propertiesMap, PASSWORD_AUTH_PROP));
break;
case BEARER:
resolvedAuthProperties.put(
ACCESS_TOKEN_AUTH_PROP, getProperty(propertiesMap, ACCESS_TOKEN_AUTH_PROP));
break;
case CLIENT_CREDENTIAL:
resolvedAuthProperties.put(CLIENT_ID_AUTH_PROP, getProperty(propertiesMap, CLIENT_ID_AUTH_PROP));
resolvedAuthProperties.put(
CLIENT_SECRET_AUTH_PROP, getProperty(propertiesMap, CLIENT_SECRET_AUTH_PROP));
resolvedAuthProperties.put(SCOPE_AUTH_PROP, getProperty(propertiesMap, SCOPE_AUTH_PROP));
break;
case API_KEY:
resolvedAuthProperties.put(HEADER_AUTH_PROP, getProperty(propertiesMap, HEADER_AUTH_PROP));
resolvedAuthProperties.put(VALUE_AUTH_PROP, getProperty(propertiesMap, VALUE_AUTH_PROP));
break;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log Improvement Suggestion No: 1

Suggested change
public Authentication build() {
switch (authType) {
case BASIC:
resolvedAuthProperties.put(USERNAME_AUTH_PROP, getProperty(propertiesMap, USERNAME_AUTH_PROP));
resolvedAuthProperties.put(PASSWORD_AUTH_PROP, getProperty(propertiesMap, PASSWORD_AUTH_PROP));
break;
case BEARER:
resolvedAuthProperties.put(
ACCESS_TOKEN_AUTH_PROP, getProperty(propertiesMap, ACCESS_TOKEN_AUTH_PROP));
break;
case CLIENT_CREDENTIAL:
resolvedAuthProperties.put(CLIENT_ID_AUTH_PROP, getProperty(propertiesMap, CLIENT_ID_AUTH_PROP));
resolvedAuthProperties.put(
CLIENT_SECRET_AUTH_PROP, getProperty(propertiesMap, CLIENT_SECRET_AUTH_PROP));
resolvedAuthProperties.put(SCOPE_AUTH_PROP, getProperty(propertiesMap, SCOPE_AUTH_PROP));
break;
case API_KEY:
resolvedAuthProperties.put(HEADER_AUTH_PROP, getProperty(propertiesMap, HEADER_AUTH_PROP));
resolvedAuthProperties.put(VALUE_AUTH_PROP, getProperty(propertiesMap, VALUE_AUTH_PROP));
break;
public Authentication build() {
switch (authType) {
case BASIC:
resolvedAuthProperties.put(USERNAME_AUTH_PROP, getProperty(propertiesMap, USERNAME_AUTH_PROP));
resolvedAuthProperties.put(PASSWORD_AUTH_PROP, getProperty(propertiesMap, PASSWORD_AUTH_PROP));
log.info("Building authentication configuration with type: BASIC");
break;
case BEARER:
resolvedAuthProperties.put(
ACCESS_TOKEN_AUTH_PROP, getProperty(propertiesMap, ACCESS_TOKEN_AUTH_PROP));
log.info("Building authentication configuration with type: BEARER");
break;
case CLIENT_CREDENTIAL:
resolvedAuthProperties.put(CLIENT_ID_AUTH_PROP, getProperty(propertiesMap, CLIENT_ID_AUTH_PROP));
resolvedAuthProperties.put(
CLIENT_SECRET_AUTH_PROP, getProperty(propertiesMap, CLIENT_SECRET_AUTH_PROP));
resolvedAuthProperties.put(SCOPE_AUTH_PROP, getProperty(propertiesMap, SCOPE_AUTH_PROP));
log.info("Building authentication configuration with type: CLIENT_CREDENTIAL");
break;
case API_KEY:
resolvedAuthProperties.put(HEADER_AUTH_PROP, getProperty(propertiesMap, HEADER_AUTH_PROP));
resolvedAuthProperties.put(VALUE_AUTH_PROP, getProperty(propertiesMap, VALUE_AUTH_PROP));
log.info("Building authentication configuration with type: API_KEY");
break;
case NONE:
log.info("Building authentication configuration with type: NONE");
break;

Comment on lines +148 to +160
// Throw an error and properly handle in the API server level
private String getProperty(Map<String, String> actionEndpointProperties, String propName) {

if (actionEndpointProperties != null && actionEndpointProperties.containsKey(propName)) {
String propValue = actionEndpointProperties.get(propName);
if (StringUtils.isNotBlank(propValue)) {
return propValue;
}
throw new IllegalArgumentException(String.format("The Property %s cannot be blank.", propName));
}

throw new NoSuchElementException(String.format("The property %s must be provided as an authentication " +
"property for the %s authentication type.", propName, authType.name()));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log Improvement Suggestion No: 2

Suggested change
// Throw an error and properly handle in the API server level
private String getProperty(Map<String, String> actionEndpointProperties, String propName) {
if (actionEndpointProperties != null && actionEndpointProperties.containsKey(propName)) {
String propValue = actionEndpointProperties.get(propName);
if (StringUtils.isNotBlank(propValue)) {
return propValue;
}
throw new IllegalArgumentException(String.format("The Property %s cannot be blank.", propName));
}
throw new NoSuchElementException(String.format("The property %s must be provided as an authentication " +
"property for the %s authentication type.", propName, authType.name()));
private String getProperty(Map<String, String> actionEndpointProperties, String propName) {
if (actionEndpointProperties != null && actionEndpointProperties.containsKey(propName)) {
String propValue = actionEndpointProperties.get(propName);
if (StringUtils.isNotBlank(propValue)) {
if (log.isDebugEnabled()) {
log.debug("Retrieved property: " + propName + " for authentication type: " + authType.name());
}
return propValue;
}
log.error("The Property " + propName + " cannot be blank.");
throw new IllegalArgumentException(String.format("The Property %s cannot be blank.", propName));
}
log.error("Missing required property: " + propName + " for authentication type: " + authType.name());
throw new NoSuchElementException(String.format("The property %s must be provided as an authentication " +
"property for the %s authentication type.", propName, authType.name()));

Comment on lines +151 to +153
return tokenManager.getToken();
} catch (Exception e) {
throw new IllegalArgumentException("Error while acquiring token for the SMS sender", e);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log Improvement Suggestion No: 3

Suggested change
return tokenManager.getToken();
} catch (Exception e) {
throw new IllegalArgumentException("Error while acquiring token for the SMS sender", e);
} catch (Exception e) {
log.error("Error while acquiring token for the SMS sender: " + e.getMessage());
throw new IllegalArgumentException("Error while acquiring token for the SMS sender", e);

Comment on lines +234 to +235

SMSSenderDTO smsSenderDTO = new SMSSenderDTO();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log Improvement Suggestion No: 4

Suggested change
SMSSenderDTO smsSenderDTO = new SMSSenderDTO();
SMSSenderDTO smsSenderDTO = new SMSSenderDTO();
log.debug("Building SMS sender configuration with name: " + this.name);
if (StringUtils.isNotEmpty(authType)) {

Comment on lines +213 to +216
public static void addAuthenticationProperties(Map<String, String> mapToBeUpdated, Authentication authentication) {

if (authentication != null) {
mapToBeUpdated.put(AUTH_TYPE_PREFIX, authentication.getType().name());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log Improvement Suggestion No: 5

Suggested change
public static void addAuthenticationProperties(Map<String, String> mapToBeUpdated, Authentication authentication) {
if (authentication != null) {
mapToBeUpdated.put(AUTH_TYPE_PREFIX, authentication.getType().name());
public static void addAuthenticationProperties(Map<String, String> mapToBeUpdated, Authentication authentication) {
if (authentication != null) {
log.debug("Adding authentication properties for type: " + authentication.getType().name());
mapToBeUpdated.put(AUTH_TYPE_PREFIX, authentication.getType().name());

Comment on lines +509 to +511

try {
SMSSenderDTO smsSenderDTO = smsSenderBuilder.build();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log Improvement Suggestion No: 6

Suggested change
try {
SMSSenderDTO smsSenderDTO = smsSenderBuilder.build();
try {
SMSSenderDTO smsSenderDTO = smsSenderBuilder.build();
if (log.isDebugEnabled()) {
log.debug("Built SMS sender configuration for: " + smsSenderDTO.getName());
}
internalAuthProp.keySet().forEach(internalAuthProperty ->

Comment on lines +46 to +53

public String getToken() throws TokenHandlerException {

// try with refresh, if any error new token
TokenRequestContext tokenRequestContext = buildTokenRequestContext(authentication);
tokenAcquirerService.setTokenRequestContext(tokenRequestContext);
TokenResponse tokenResponse = tokenAcquirerService.getNewAccessToken();
return tokenResponse.getAccessToken();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log Improvement Suggestion No: 7

Suggested change
public String getToken() throws TokenHandlerException {
// try with refresh, if any error new token
TokenRequestContext tokenRequestContext = buildTokenRequestContext(authentication);
tokenAcquirerService.setTokenRequestContext(tokenRequestContext);
TokenResponse tokenResponse = tokenAcquirerService.getNewAccessToken();
return tokenResponse.getAccessToken();
public String getToken() throws TokenHandlerException {
log.info("Initiating token acquisition process");
// try with refresh, if any error new token
TokenRequestContext tokenRequestContext = buildTokenRequestContext(authentication);
tokenAcquirerService.setTokenRequestContext(tokenRequestContext);
TokenResponse tokenResponse = tokenAcquirerService.getNewAccessToken();
if (log.isDebugEnabled()) {
log.debug("Successfully acquired new access token");
}
return tokenResponse.getAccessToken();
}

Comment on lines +57 to +72

private TokenRequestContext buildTokenRequestContext(Authentication authentication) throws TokenHandlerException {

// only for credentials grant type <= think where this check should go
// properly set the grant context details with switch
GrantContext grantContext = new GrantContext.Builder()
.grantType(GrantContext.GrantType.CLIENT_CREDENTIAL)
.properties(authentication.getProperties())
.build();

// get endpoint url from prop
TokenRequestContext.Builder builder = new TokenRequestContext.Builder()
.grantContext(grantContext)
.endpointUrl("https://customauth.free.beeceptor.com/todos");

return builder.build();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Log Improvement Suggestion No: 8

Suggested change
private TokenRequestContext buildTokenRequestContext(Authentication authentication) throws TokenHandlerException {
// only for credentials grant type <= think where this check should go
// properly set the grant context details with switch
GrantContext grantContext = new GrantContext.Builder()
.grantType(GrantContext.GrantType.CLIENT_CREDENTIAL)
.properties(authentication.getProperties())
.build();
// get endpoint url from prop
TokenRequestContext.Builder builder = new TokenRequestContext.Builder()
.grantContext(grantContext)
.endpointUrl("https://customauth.free.beeceptor.com/todos");
return builder.build();
private TokenRequestContext buildTokenRequestContext(Authentication authentication) throws TokenHandlerException {
if (log.isDebugEnabled()) {
log.debug("Building token request context for grant type: CLIENT_CREDENTIAL");
}
// only for credentials grant type <= think where this check should go
// properly set the grant context details with switch
GrantContext grantContext = new GrantContext.Builder()
.grantType(GrantContext.GrantType.CLIENT_CREDENTIAL)
.properties(authentication.getProperties())
.build();
// get endpoint url from prop
TokenRequestContext.Builder builder = new TokenRequestContext.Builder()
.grantContext(grantContext)
.endpointUrl("https://customauth.free.beeceptor.com/todos");
return builder.build();
}

Copy link

@wso2-engineering wso2-engineering bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI Agent Log Improvement Checklist

⚠️ Warning: AI-Generated Review Comments

  • The log-related comments and suggestions in this review were generated by an AI tool to assist with identifying potential improvements. Purpose of reviewing the code for log improvements is to improve the troubleshooting capabilities of our products.
  • Please make sure to manually review and validate all suggestions before applying any changes. Not every code suggestion would make sense or add value to our purpose. Therefore, you have the freedom to decide which of the suggestions are helpful.

✅ Before merging this pull request:

  • Review all AI-generated comments for accuracy and relevance.
  • Complete and verify the table below. We need your feedback to measure the accuracy of these suggestions and the value they add. If you are rejecting a certain code suggestion, please mention the reason briefly in the suggestion for us to capture it.
Comment Accepted (Y/N) Reason
#### Log Improvement Suggestion No: 1
#### Log Improvement Suggestion No: 4
#### Log Improvement Suggestion No: 5
#### Log Improvement Suggestion No: 6
#### Log Improvement Suggestion No: 8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant