This repository has been archived by the owner on May 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
355 additions
and
12 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
Binary file not shown.
Binary file not shown.
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,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(); | ||
} | ||
|
||
} |
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,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; | ||
} | ||
|
||
} |
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,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")); | ||
} | ||
} |
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
Oops, something went wrong.