Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
Add start phone verification
Browse files Browse the repository at this point in the history
  • Loading branch information
moisesvw committed Nov 17, 2015
1 parent 51348bb commit afb2398
Show file tree
Hide file tree
Showing 15 changed files with 355 additions and 12 deletions.
10 changes: 8 additions & 2 deletions build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
</target>

<target name="compile" depends="init">
<javac srcdir="src/main/java" destdir="build/classes" />
<javac srcdir="src/main/java" destdir="build/classes">
<classpath>
<pathelement path="lib/json-20150729.jar"/>
</classpath>
</javac>
</target>

<target name="compress" depends="compile">
<jar destfile="dist/authy-java.jar"
basedir="build/classes"
excludes="**/Main*.class" />
excludes="**/Main*.class">
<zipgroupfileset dir="lib" includes="json-20150729.jar" />
</jar>
</target>

<target name="clean">
Expand Down
Binary file added dist/authy-java.jar
Binary file not shown.
Binary file added lib/json-20150729.jar
Binary file not shown.
100 changes: 100 additions & 0 deletions src/com/authy/api/Phone.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package com.authy.api;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.*;

/**
*
* @author Moisés Vargas
*
*/
@XmlRootElement(name="verification")
public class Phone implements Response {
String number;
String countryCode;
String locale;
String via;

public Phone() {}

public Phone(String number, String countryCode, String locale, String via) {
this.number = number;
this.countryCode = countryCode;
this.locale = locale;
this.via = via;
}

@XmlElement(name="phone_number")
public String getNumber() {
return number;
}

@XmlElement(name="country_code")
public String getCountryCode() {
return countryCode;
}

@XmlElement(name="locale")
public String getLocale() {
return locale;
}

@XmlElement(name="via")
public String getVia() {
return via;
}

/**
* Map a Token instance to its XML representation.
* @return a String with the description of this object in XML.
*/
public String toXML() {
StringWriter sw = new StringWriter();
String xml = "";
try {
JAXBContext context = JAXBContext.newInstance(this.getClass());
Marshaller marshaller = context.createMarshaller();

marshaller.marshal(this, sw);
xml = sw.toString();
}catch(Exception e) {
e.printStackTrace();
}

return xml.toString();
}

/**
* Map a Token instance to its Java's Map representation.
* @return a Java's Map with the description of this object.
*/
public Map<String, String> toMap() {
Map<String, String> map = new HashMap<String, String>();

map.put("phone_number", number);
map.put("country_code", countryCode);
map.put("locale", locale);
map.put("via", via);

return map;
}

public String toJSON(){
org.json.JSONObject verification = new org.json.JSONObject();

verification.put("phone_number", this.number);
verification.put("country_code", this.countryCode);
verification.put("locale", this.locale);
verification.put("via", this.via);

return verification.toString();
}

}
50 changes: 50 additions & 0 deletions src/com/authy/api/PhoneVerification.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.authy.api;

import java.io.StringReader;
import java.net.URLEncoder;
import java.util.Map;
import java.util.HashMap;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;

import com.authy.AuthyException;

/**
*
* @author Moisés Vargas
*
*/
public class PhoneVerification extends Resource {
public static final String START_VERIFICATION_CODE_PATH = "/protected/json/phones/verification/";

public PhoneVerification(String uri, String key) {
super(uri, key, "JSON");
}

public PhoneVerification(String uri, String key, boolean testFlag) {
super(uri, key, testFlag, "JSON");
}

public Verification start(Phone phone) {
Verification verification = new Verification();
StringBuffer path = new StringBuffer(START_VERIFICATION_CODE_PATH);
String response = "";

try {
path.append("start");
response = this.post(path.toString(), phone);

verification.setStatus(this.getStatus());
verification.setResponse(response);
}

catch(Exception e) {
e.printStackTrace();
}
return verification;
}

}
126 changes: 126 additions & 0 deletions src/com/authy/api/Verification.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
package com.authy.api;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

/**
*
* @author Moisés Vargas
*
*/
@XmlRootElement(name="verification")
public class Verification implements Response {
private int status = 503;
private String response;
private org.json.JSONObject jsonResponse;
private String message = "Something went wrong!";
private boolean isPorted = false;
private boolean isCellphone = false;

public Verification() {
}

public Verification(int status, String response, String message) {
this.status = status;
this.response = response;
this.message = message;
}

@XmlElement(name="message")
public String getMessage() {
return message;
}

@XmlElement(name="success")
public String getSuccess(){
return Boolean.toString(this.isOk());
}

@XmlElement(name="is_ported")
public String getIsPorted(){
return Boolean.toString(this.isPorted);
}

@XmlElement(name="is_cellphone")
public String getIsCellphone(){
return Boolean.toString(this.isCellphone);
}

public void setStatus(int status) {
this.status = status;
}

public void setResponse(String response) {
this.response = response;
this.jsonResponse = new org.json.JSONObject (response);
this.parseResponseToOjbect(jsonResponse);
}

public boolean isOk() {
return status == 200;
}

/**
* Map a Token instance to its XML representation.
* @return a String with the description of this object in XML.
*/
public String toXML() {
StringWriter sw = new StringWriter();
String xml = "";

try {
JAXBContext context = JAXBContext.newInstance(this.getClass());
Marshaller marshaller = context.createMarshaller();

marshaller.marshal(this, sw);
xml = sw.toString();
}
catch(Exception e) {
e.printStackTrace();
}
return xml;
}

/**
* Map a Token instance to its Java's Map representation.
* @return a Java's Map with the description of this object.
*/
public Map<String, String> toMap() {
Map<String, String> map = new HashMap<String, String>();

map.put("message", this.getMessage());
map.put("success", this.getSuccess());
map.put("is_ported", this.getIsPorted());
map.put("is_cellphone", this.getIsCellphone());

return map;
}

public String toJSON(){
org.json.JSONObject verification = new org.json.JSONObject();

verification.put("message", this.getMessage());
verification.put("success", this.getSuccess());
verification.put("is_ported", this.getIsPorted());
verification.put("is_cellphone", this.getIsCellphone());

return verification.toString();
}

private void parseResponseToOjbect(org.json.JSONObject json){
if( !json.isNull("message") )
this.message = json.getString("message");

if( !json.isNull("is_ported") )
this.isPorted = Boolean.parseBoolean(json.getString("is_ported"));

if( !json.isNull("is_cellphone") )
this.isCellphone = Boolean.parseBoolean(json.getString("is_cellphone"));
}
}
19 changes: 14 additions & 5 deletions src/main/java/com/authy/AuthyApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,31 +11,36 @@ public class AuthyApiClient {
private Users users;
private Tokens tokens;
private String apiUri, apiKey;
private Phone phone;
private PhoneVerification phoneVerification;

public static final String CLIENT_NAME = "AuthyJava";
public static final String CLIENT_NAME = "AuthyJava";
public static final String DEFAULT_API_URI = "https://api.authy.com";
public static final String VERSION = "1.0.1";
public static final String VERSION = "1.0.1";

public AuthyApiClient(String apiKey, String apiUri) {
this.apiUri = apiUri;
this.apiKey = apiKey;


this.phoneVerification = new PhoneVerification(this.apiUri, this.apiKey);
this.users = new Users(this.apiUri, this.apiKey);
this.tokens = new Tokens(this.apiUri, this.apiKey);
}

public AuthyApiClient(String apiKey) {
this.apiUri = DEFAULT_API_URI;
this.apiKey = apiKey;


this.phoneVerification = new PhoneVerification(this.apiUri, this.apiKey);
this.users = new Users(this.apiUri, this.apiKey);
this.tokens = new Tokens(this.apiUri, this.apiKey);
}

public AuthyApiClient(String apiKey, String apiUri, boolean testFlag) {
this.apiUri = apiUri;
this.apiKey = apiKey;


this.phoneVerification = new PhoneVerification(this.apiUri, this.apiKey, testFlag);
this.users = new Users(this.apiUri, this.apiKey, testFlag);
this.tokens = new Tokens(this.apiUri, this.apiKey, testFlag);
}
Expand All @@ -47,4 +52,8 @@ public Users getUsers() {
public Tokens getTokens() {
return this.tokens;
}

public PhoneVerification getPhoneVerification() {
return this.phoneVerification;
}
}
1 change: 1 addition & 0 deletions src/main/java/com/authy/api/Error.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public Map<String, String> toMap() {
return map;
}

public String toJSON(){ return ""; }
@Override
public String toString() {
return "Error [message=" + message + ", url=" + url + ", countryCode="
Expand Down
1 change: 1 addition & 0 deletions src/main/java/com/authy/api/Hash.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ public String toXML() {
return xml;
}

public String toJSON(){ return ""; }
/**
* Map a Token instance to its Java's Map representation.
* @return a Java's Map with the description of this object.
Expand Down
Loading

0 comments on commit afb2398

Please sign in to comment.