public AuthCollection(AuthMethod... authMethods) { this(); for (AuthMethod method : authMethods) { add(method); } }
public HttpWrapper(HttpConfig httpConfig, AuthMethod... authMethods) { this(new AuthCollection()); for (AuthMethod authMethod : authMethods) { authCollection.add(authMethod); } this.httpConfig = httpConfig; }
/** * Calculates MD5 hash for string. assume string is UTF-8 encoded * @param input string which is going to be encoded into MD5 format * @return MD5 representation of the input string * @throws NoSuchAlgorithmException if the MD5 algorithm is not available. */ public static String calculateMd5(String input) throws NoSuchAlgorithmException { try { return calculateMd5(input, "UTF-8"); } catch (UnsupportedEncodingException e) { return null; // -- impossible -- } }
@Test public void testAuthMethodPrecedence() throws Exception { JWTAuthMethod jAuth = new JWTAuthMethod("application_id", testUtils.loadKey("test/keys/application_key")); TokenAuthMethod tAuth = new TokenAuthMethod("key", "secret"); AuthCollection auths = new AuthCollection(); auths.add(tAuth); auths.add(jAuth); Set<Class> acceptableAuths = new HashSet<>(); acceptableAuths.add(JWTAuthMethod.class); assertEquals(jAuth, auths.getAcceptableAuthMethod(acceptableAuths)); }
@Test public void testMultipleAuthMethods() throws Exception { JWTAuthMethod jwtAuth = new JWTAuthMethod("application_id", testUtils.loadKey("test/keys/application_key")); TokenAuthMethod tokenAuth = new TokenAuthMethod("api_key", "api_secret"); AuthCollection auths = new AuthCollection( jwtAuth, tokenAuth ); assertEquals(jwtAuth, auths.getAcceptableAuthMethod(acceptableClassSet(JWTAuthMethod.class))); assertEquals(tokenAuth, auths.getAcceptableAuthMethod(acceptableClassSet(TokenAuthMethod.class))); }
@Test public void testGetAcceptableAuthMethod() throws Exception { JWTAuthMethod jAuth = new JWTAuthMethod("application_id", testUtils.loadKey("test/keys/application_key")); AuthCollection auths = new AuthCollection(); auths.add(jAuth); Set<Class> acceptableAuths = acceptableClassSet(JWTAuthMethod.class); assertEquals(jAuth, auths.getAcceptableAuthMethod(acceptableAuths)); }
@Test public void testIncompatibleAuths() throws Exception { TokenAuthMethod tAuth = new TokenAuthMethod("key", "secret"); AuthCollection auths = new AuthCollection(); auths.add(tAuth); Set<Class> acceptableAuths = new HashSet<>(); acceptableAuths.add(JWTAuthMethod.class); try { auths.getAcceptableAuthMethod(acceptableAuths); fail("No acceptable auth method should throw a NexmoClientException"); } catch (NexmoUnacceptableAuthException ex) { assertEquals("No acceptable authentication type could be found. Acceptable types are: JWTAuthMethod. Supplied " + "types were: TokenAuthMethod", ex.getMessage()); } }
/** * Generate a JWT for the application the client has been configured with. * * @return A String containing the token data. * * @throws NexmoUnacceptableAuthException if no {@link JWTAuthMethod} is available */ public String generateJwt() throws NexmoUnacceptableAuthException { JWTAuthMethod authMethod = this.httpWrapper.getAuthCollection().getAuth(JWTAuthMethod.class); return authMethod.constructToken(System.currentTimeMillis() / 1000L, JWTAuthMethod.constructJTI()); }
@Test public void testVerifyRequestSignatureHandlesNullParams() { Map<String, String[]> params = constructDummyParams(); params.put("b", new String[]{ null }); params.put("sig", new String[]{"a3368bf718ba104dcb392d8877e8eb2b"}); HttpServletRequest request = constructDummyRequest(params); assertTrue(RequestSigning.verifyRequestSignature(request, "abcde", 2100000)); }
@Test public void testVerifyRequestSignature() { HttpServletRequest request = constructDummyRequest(); assertTrue(RequestSigning.verifyRequestSignature(request, "abcde", 2100000)); }
@Override public int compareTo(AuthMethod other) { return Integer.compare(this.getSortKey(), other.getSortKey()); }
/** * Verifies the signature in an HttpServletRequest. * * @param request The HttpServletRequest to be verified * @param secretKey The pre-shared secret key used by the sender of the request to create the signature * * @return true if the signature is correct for this request and secret key. */ public static boolean verifyRequestSignature(HttpServletRequest request, String secretKey) { return verifyRequestSignature(request, secretKey, System.currentTimeMillis()); }
/** * Signs a set of request parameters. * <p> * Generates additional parameters to represent the timestamp and generated signature. * Uses the supplied pre-shared secret key to generate the signature. * * @param params List of NameValuePair instances containing the query parameters for the request that is to be signed * @param secretKey the pre-shared secret key held by the client * */ public static void constructSignatureForRequestParameters(List<NameValuePair> params, String secretKey) { constructSignatureForRequestParameters(params, secretKey, System.currentTimeMillis() / 1000); }
@Override protected RequestBuilder applyAuth(RequestBuilder request) throws NexmoClientException { return getAuthMethod(getAcceptableAuthMethods()).applyAsBasicAuth(request); } }
public String getMessage() { return generateErrorMessage(); }
private HttpServletRequest constructDummyRequest() { return constructDummyRequest(null); }
@Override protected RequestBuilder applyAuth(RequestBuilder request) throws NexmoClientException { return getAuthMethod(getAcceptableAuthMethods()).applyAsBasicAuth(request); } }
@Override protected RequestBuilder applyAuth(RequestBuilder request) throws NexmoClientException { return getAuthMethod(getAcceptableAuthMethods()).applyAsBasicAuth(request); } }
@Override protected RequestBuilder applyAuth(RequestBuilder request) throws NexmoClientException { return getAuthMethod(getAcceptableAuthMethods()).applyAsBasicAuth(request); } }
@Override protected RequestBuilder applyAuth(RequestBuilder request) throws NexmoClientException { return getAuthMethod(getAcceptableAuthMethods()).applyAsBasicAuth(request); } }