-
-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added SsoToken class with common code for retrieving token info from …
…API response.
- Loading branch information
1 parent
445ec10
commit b4dad76
Showing
5 changed files
with
62 additions
and
40 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
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
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,38 @@ | ||
package org.commcare.connect.network; | ||
|
||
import org.commcare.connect.ConnectConstants; | ||
import org.javarosa.core.io.StreamsUtil; | ||
import org.json.JSONException; | ||
import org.json.JSONObject; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Date; | ||
|
||
public class SsoToken { | ||
public String token; | ||
public Date expiration; | ||
|
||
public SsoToken(String token, Date expiration) { | ||
this.token = token; | ||
this.expiration = expiration; | ||
} | ||
|
||
public static SsoToken fromResponseStream(InputStream stream) throws IOException, JSONException { | ||
String responseAsString = new String(StreamsUtil.inputStreamToByteArray( | ||
stream)); | ||
JSONObject json = new JSONObject(responseAsString); | ||
String key = ConnectConstants.CONNECT_KEY_TOKEN; | ||
|
||
if (!json.has(key)) { | ||
throw new RuntimeException("SSO API response missing access token"); | ||
} | ||
String token = json.getString(key); | ||
Date expiration = new Date(); | ||
key = ConnectConstants.CONNECT_KEY_EXPIRES; | ||
int seconds = json.has(key) ? json.getInt(key) : 0; | ||
expiration.setTime(expiration.getTime() + ((long)seconds * 1000)); | ||
|
||
return new SsoToken(token, expiration); | ||
} | ||
} |