Skip to content

Commit 7306f8a

Browse files
authored
Enrollment for TOTP & SMS (#1)
Enrollment for TOTP & SMS
1 parent d86b67f commit 7306f8a

39 files changed

Lines changed: 3038 additions & 0 deletions

CHANGELOG.md

Whitespace-only changes.

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2017 Auth0, Inc. <support@auth0.com> (http://auth0.com)
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,111 @@ or Gradle:
2626
compile 'com.auth0:guardian:0.0.1'
2727
```
2828

29+
## Usage
30+
31+
Create an instance of `Guardian` using you Guardian URL:
32+
33+
```java
34+
Guardian guardian = new Guardian("https://<tenant>.guardian.auth0.com");
35+
```
36+
37+
Obtain an enrollment ticket from API2:
38+
39+
```java
40+
String enrollmentTicket = "Ag1qX7vZVBvyTKhFwrkzaCH2M8vn5b6c";
41+
```
42+
43+
### Enrollment
44+
45+
#### TOTP
46+
47+
Use the ticket and `EnrollmentType.TOTP()` to request an TOTP enrollment.
48+
For TOTP you must ask for the TOTP URI to show to the user in the QR code.
49+
50+
```java
51+
Transaction enrollmentTransaction;
52+
try {
53+
enrollmentTransaction = guardian
54+
.requestEnroll(enrollmentTicket, EnrollmentType.TOTP());
55+
56+
// Only for TOTP: use the TOTP URI to create a QR and scan with an app
57+
String totpURI = enrollmentTransaction.totpURI("Username", "Issuer");
58+
System.out.println(totpURI);
59+
60+
} catch (IOException e) {
61+
// connection issue, might be internet (or invalid certificates for example)
62+
} catch (GuardianException e) {
63+
if (e.isAlreadyEnrolled()) {
64+
// the user was already enrolled
65+
} else if (e.isInvalidToken()) {
66+
// the ticket is not valid anymore, or was already used
67+
} else {
68+
// some other guardian error, check the message
69+
}
70+
}
71+
```
72+
73+
#### SMS
74+
75+
For SMS use `EnrollmentType.SMS()` and the phone number instead:
76+
77+
```java
78+
Transaction enrollmentTransaction;
79+
try {
80+
enrollmentTransaction = guardian
81+
.requestEnroll(enrollmentTicket, EnrollmentType.SMS("+5493424217158"));
82+
83+
} catch (IOException e) {
84+
// connection issue, might be internet (or invalid certificates for example)
85+
} catch (GuardianException e) {
86+
if (e.isAlreadyEnrolled()) {
87+
// the user was already enrolled
88+
} else if (e.isInvalidToken()) {
89+
// the ticket is not valid anymore, or was already used
90+
} else {
91+
// some other guardian error, check the message
92+
}
93+
}
94+
```
95+
96+
### Transaction storage
97+
98+
`Transaction` implements `java.io.Serializable` interface so you can save and restore it easily.
99+
100+
> The transaction contains sensitive information like the transaction token and the recovery code. Keep in mind this
101+
> when considering possible storage options.
102+
103+
### Confirm enrollment
104+
105+
Restore the enrollment transaction from wherever you saved it, and use it together with the OTP that the user inputs to
106+
confirm the enrollment, whether it's TOTP or SMS.
107+
108+
If the OTP was valid, the enrollment is confirmed and you get an object that contains the recovery code.
109+
110+
```java
111+
// get the OTP from SMS or TOTP app
112+
String code = "123456";
113+
114+
try {
115+
Enrollment enrollment = guardian.confirmEnroll(enrollmentTransaction, code);
116+
117+
// Get the recovery code and show to the user
118+
String recoveryCode = enrollment.getRecoveryCode();
119+
System.out.println(recoveryCode);
120+
121+
} catch (IOException e) {
122+
// connection issue, might be internet (or invalid certificates for example)
123+
} catch (GuardianException e) {
124+
if (e.isInvalidToken()) {
125+
// the transaction is not valid anymore
126+
} else if (e.isInvalidOTP()) {
127+
// the OTP is not valid
128+
} else {
129+
// some other guardian error, check the message
130+
}
131+
}
132+
```
133+
29134
## Documentation
30135

31136
For more information about [auth0](http://auth0.com) check our [documentation page](http://docs.auth0.com/).

build.gradle

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
group = 'com.auth0'
2+
3+
apply plugin: 'com.auth0.gradle.oss-library.java'
4+
apply plugin: 'jacoco'
5+
6+
logger.lifecycle("Using version ${version} for ${name}")
7+
8+
oss {
9+
name 'guardian'
10+
repository 'guardian-java'
11+
organization 'auth0'
12+
description 'Java library for Auth0\'s Guardian platform.'
13+
14+
developers {
15+
auth0 {
16+
displayName = 'Auth0'
17+
email = 'oss@auth0.com'
18+
}
19+
nikolaseu {
20+
displayName = 'Nicolas Ulrich'
21+
email = 'nicolas.ulrich@auth0.com'
22+
}
23+
}
24+
}
25+
26+
jacocoTestReport {
27+
reports {
28+
xml.enabled = true
29+
html.enabled = true
30+
}
31+
}
32+
33+
compileJava {
34+
sourceCompatibility '1.7'
35+
targetCompatibility '1.7'
36+
}
37+
38+
buildscript {
39+
repositories {
40+
maven {
41+
url "https://plugins.gradle.org/m2/"
42+
}
43+
}
44+
dependencies {
45+
classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.7'
46+
classpath "gradle.plugin.com.auth0.gradle:oss-library:0.6.0"
47+
}
48+
}
49+
50+
repositories {
51+
mavenCentral()
52+
}
53+
54+
test {
55+
testLogging {
56+
events "skipped", "failed"
57+
exceptionFormat "short"
58+
}
59+
}
60+
61+
dependencies {
62+
compile 'com.squareup.okhttp3:okhttp:3.6.0'
63+
compile 'com.squareup.okhttp3:logging-interceptor:3.6.0'
64+
compile 'com.fasterxml.jackson.core:jackson-databind:2.8.7'
65+
66+
testCompile 'org.mockito:mockito-core:2.5.4'
67+
testCompile 'com.squareup.okhttp3:mockwebserver:3.6.0'
68+
testCompile 'org.hamcrest:java-hamcrest:2.0.0.0'
69+
testCompile 'junit:junit:4.11'
70+
}

gradle/wrapper/gradle-wrapper.jar

52.3 KB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#Tue Jan 03 13:42:16 ART 2017
2+
distributionBase=GRADLE_USER_HOME
3+
distributionPath=wrapper/dists
4+
zipStoreBase=GRADLE_USER_HOME
5+
zipStorePath=wrapper/dists
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.13-all.zip

gradlew

Lines changed: 164 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)