Skip to content

Commit dbd42ed

Browse files
committed
Review
1 parent d5a2e07 commit dbd42ed

File tree

2 files changed

+29
-19
lines changed

2 files changed

+29
-19
lines changed

src/main/java/com/authlete/jaxrs/server/api/CredentialOfferIssueEndpoint.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,18 +71,16 @@ public Response post(
7171
final AuthleteApi api = AuthleteApiFactory.getDefaultApi();
7272
final User user = ProcessingUtil.getUser(session, parameters);
7373

74-
if(user == null)
74+
if (user == null)
7575
{
7676
throw ExceptionUtil.badRequestException("Bad authentication.");
7777
}
7878

7979
final CredentialOfferCreateRequest createRequest = model.toRequest(user);
8080
final CredentialOfferCreateResponse response = api.credentialOfferCreate(createRequest);
8181

82-
switch(response.getAction())
82+
switch (response.getAction())
8383
{
84-
default:
85-
throw ExceptionUtil.badRequestException("An exception occured: " + response.getResultMessage());
8684
case CREATED:
8785
model.setInfo(response.getInfo());
8886
model.setUser(user);
@@ -93,6 +91,9 @@ public Response post(
9391

9492
// Create a response that has the viewable as its content.
9593
return Response.ok(viewable, MediaType.TEXT_HTML_TYPE.withCharset("UTF-8")).build();
94+
95+
default:
96+
throw ExceptionUtil.badRequestException("An exception occured: " + response.getResultMessage());
9697
}
9798
}
9899
}

src/main/java/com/authlete/jaxrs/server/api/CredentialOfferPageModel.java

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import java.net.URLEncoder;
2323
import java.util.Base64;
2424
import java.util.Map;
25+
import java.util.function.Consumer;
2526
import javax.imageio.ImageIO;
2627
import com.authlete.common.dto.CredentialOfferCreateRequest;
2728
import com.authlete.common.dto.CredentialOfferInfo;
@@ -47,6 +48,8 @@ public class CredentialOfferPageModel extends AuthorizationPageModel
4748
private static final String DEFAULT_ENDPOINT = "openid-credential-offer://";
4849
private static final String CREDENTIAL_OFFER_QR_PATTERN = "%s?credential_offer=%s";
4950
private static final String CREDENTIAL_OFFER_URI_QR_PATTERN = "%s?credential_offer_uri=%s";
51+
private static final int QR_CODE_WIDTH = 300;
52+
public static final int QR_CODE_HEIGHT = 300;
5053

5154

5255
private static final String DEFAULT_CREDENTIALS = "[\n" +
@@ -98,36 +101,42 @@ public CredentialOfferPageModel setValues(final Map<String, String> values)
98101
this.preAuthorizedCodeGrantIncluded = ProcessingUtil.fromFormCheckbox(values, "preAuthorizedCodeGrantIncluded");
99102
this.userPinRequired = ProcessingUtil.fromFormCheckbox(values, "userPinRequired");
100103
this.credentialOfferEndpoint = values.getOrDefault("credentialOfferEndpoint", this.credentialOfferEndpoint);
104+
this.userPinLength = extractInt(values, "userPinLength", this.userPinLength);
105+
this.duration = extractInt(values, "duration", this.duration);
106+
return this;
107+
}
101108

102-
final String userPinLengthStr = values.getOrDefault("userPinLength", Integer.toString(this.userPinLength));
103-
final String durationStr = values.getOrDefault("duration", Integer.toString(this.duration));
104109

105-
try
106-
{
107-
userPinLength = Integer.parseInt(userPinLengthStr);
108-
}
109-
catch (NumberFormatException e)
110-
{
111-
throw ExceptionUtil.badRequestException("User pin length should be a number.");
112-
}
110+
private Integer extractInt(final Map<String, String> values,
111+
final String key, final Integer def)
112+
{
113+
final String value = values.getOrDefault(key, Integer.toString(def));
113114

114115
try
115116
{
116-
duration = Integer.parseInt(durationStr);
117+
final Integer intVal = Integer.parseInt(value);
118+
119+
if (intVal < 0)
120+
{
121+
throw ExceptionUtil.badRequestException(
122+
String.format("%s should be positive.", key));
123+
}
124+
125+
return intVal;
117126
}
118127
catch (NumberFormatException e)
119128
{
120-
throw ExceptionUtil.badRequestException("Duration should be a number.");
129+
throw ExceptionUtil.badRequestException(
130+
String.format("%s should be a number.", key));
121131
}
122-
123-
return this;
124132
}
125133

126134

127135
private String asQrCode(final String text) throws IOException, WriterException
128136
{
129137
final QRCodeWriter qrCodeWriter = new QRCodeWriter();
130-
final BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, 300, 300);
138+
final BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE,
139+
QR_CODE_WIDTH, QR_CODE_HEIGHT);
131140

132141
final BufferedImage qrCode = MatrixToImageWriter.toBufferedImage(bitMatrix);
133142

0 commit comments

Comments
 (0)