-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(jans-auth-server): added flexible date formatter handler to AS (…
- Loading branch information
Showing
12 changed files
with
195 additions
and
89 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
jans-auth-server/model/src/main/java/io/jans/as/model/common/CallerType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package io.jans.as.model.common; | ||
|
||
/** | ||
* @author Yuriy Z | ||
*/ | ||
public enum CallerType { | ||
COMMON, | ||
AUTHORIZE, | ||
USERINFO, | ||
ID_TOKEN | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
24 changes: 0 additions & 24 deletions
24
jans-auth-server/model/src/main/java/io/jans/as/model/userinfo/UserInfoConfiguration.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
63 changes: 63 additions & 0 deletions
63
...auth-server/server/src/main/java/io/jans/as/server/service/date/DateFormatterService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package io.jans.as.server.service.date; | ||
|
||
import io.jans.as.model.common.CallerType; | ||
import io.jans.as.model.configuration.AppConfiguration; | ||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
import jakarta.inject.Named; | ||
import org.apache.commons.lang.StringUtils; | ||
import org.slf4j.Logger; | ||
|
||
import java.io.Serializable; | ||
import java.text.SimpleDateFormat; | ||
import java.util.Date; | ||
import java.util.Map; | ||
|
||
/** | ||
* @author Yuriy Z | ||
*/ | ||
@ApplicationScoped | ||
@Named | ||
public class DateFormatterService { | ||
|
||
@Inject | ||
private Logger log; | ||
|
||
@Inject | ||
private AppConfiguration appConfiguration; | ||
|
||
public Serializable formatClaim(Date date, CallerType callerType) { | ||
return formatClaim(date, callerType.name().toLowerCase()); | ||
} | ||
|
||
/** | ||
* | ||
* @param date date to format | ||
* @param patternKey pattern key. It's by intention is not enum to allow arbitrary key (not "locked" by CallerType) | ||
* @return formatter value | ||
*/ | ||
public Serializable formatClaim(Date date, String patternKey) { | ||
// key in map is string by intention to not "lock" it by CallerType | ||
final Map<String, String> formatterMap = appConfiguration.getDateFormatterPatterns(); | ||
|
||
if (formatterMap.isEmpty()) { | ||
return formatClaimFallback(date); | ||
} | ||
|
||
final String explicitFormatter = formatterMap.get(patternKey); | ||
if (StringUtils.isNotBlank(explicitFormatter)) { | ||
return new SimpleDateFormat(explicitFormatter).format(date); | ||
} | ||
|
||
final String commonFormatter = formatterMap.get(CallerType.COMMON.name().toLowerCase()); | ||
if (StringUtils.isNotBlank(commonFormatter)) { | ||
return new SimpleDateFormat(commonFormatter).format(date); | ||
} | ||
|
||
return formatClaimFallback(date); | ||
} | ||
|
||
public Serializable formatClaimFallback(Date date) { | ||
return date.getTime() / 1000; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
...-server/server/src/test/java/io/jans/as/server/service/date/DateFormatterServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package io.jans.as.server.service.date; | ||
|
||
import io.jans.as.model.common.CallerType; | ||
import io.jans.as.model.configuration.AppConfiguration; | ||
import org.mockito.InjectMocks; | ||
import org.mockito.Mock; | ||
import org.mockito.Spy; | ||
import org.mockito.testng.MockitoTestNGListener; | ||
import org.slf4j.Logger; | ||
import org.testng.annotations.Listeners; | ||
import org.testng.annotations.Test; | ||
|
||
import java.io.Serializable; | ||
import java.util.Calendar; | ||
import java.util.Date; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.mockito.Mockito.when; | ||
import static org.testng.Assert.assertEquals; | ||
|
||
/** | ||
* @author Yuriy Z | ||
*/ | ||
@Listeners(MockitoTestNGListener.class) | ||
public class DateFormatterServiceTest { | ||
|
||
@InjectMocks | ||
@Spy | ||
private DateFormatterService dateFormatterService; | ||
|
||
@Mock | ||
private Logger log; | ||
|
||
@Mock | ||
private AppConfiguration appConfiguration; | ||
|
||
@Test | ||
public void formatClaim_whenNotFormatter_shouldFallback() { | ||
Date date = new Date(10000); | ||
|
||
final Serializable formattedValue = dateFormatterService.formatClaim(date, CallerType.USERINFO); | ||
assertEquals(10L, formattedValue); | ||
} | ||
|
||
@Test | ||
public void formatClaim_whenExplicitClaimFormatterIsSet_shouldFormatByFormatter() { | ||
Calendar calendar = Calendar.getInstance(); | ||
calendar.set(2023, Calendar.JANUARY, 11); | ||
|
||
Map<String, String> map = new HashMap<>(); | ||
map.put("birthdate", "yyyy-MM-dd"); | ||
|
||
when(appConfiguration.getDateFormatterPatterns()).thenReturn(map); | ||
|
||
final Serializable formattedValue = dateFormatterService.formatClaim(calendar.getTime(), "birthdate"); | ||
assertEquals(formattedValue, "2023-01-11"); | ||
} | ||
|
||
@Test | ||
public void formatClaim_whenUserInfoFormatterIsSet_shouldFormatByFormatter() { | ||
Calendar calendar = Calendar.getInstance(); | ||
calendar.set(2023, Calendar.JANUARY, 11); | ||
|
||
Map<String, String> map = new HashMap<>(); | ||
map.put("userinfo", "yyyy-MM-dd"); | ||
|
||
when(appConfiguration.getDateFormatterPatterns()).thenReturn(map); | ||
|
||
final Serializable formattedValue = dateFormatterService.formatClaim(calendar.getTime(), CallerType.USERINFO); | ||
assertEquals(formattedValue, "2023-01-11"); | ||
} | ||
|
||
@Test | ||
public void formatClaim_whenCommonFormatterIsSet_shouldFormatByFormatter() { | ||
Calendar calendar = Calendar.getInstance(); | ||
calendar.set(2023, Calendar.JANUARY, 11); | ||
|
||
Map<String, String> map = new HashMap<>(); | ||
map.put("common", "yyyy-MM-dd"); | ||
|
||
when(appConfiguration.getDateFormatterPatterns()).thenReturn(map); | ||
|
||
final Serializable formattedValue = dateFormatterService.formatClaim(calendar.getTime(), CallerType.USERINFO); | ||
assertEquals(formattedValue, "2023-01-11"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.