2222import java .net .URLEncoder ;
2323import java .util .Base64 ;
2424import java .util .Map ;
25+ import java .util .function .Consumer ;
2526import javax .imageio .ImageIO ;
2627import com .authlete .common .dto .CredentialOfferCreateRequest ;
2728import 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