Skip to content

Instantly share code, notes, and snippets.

@Titogelo
Created August 17, 2017 09:25
Show Gist options
  • Save Titogelo/1a87c562c15cdff42057e98a84e5024c to your computer and use it in GitHub Desktop.
Save Titogelo/1a87c562c15cdff42057e98a84e5024c to your computer and use it in GitHub Desktop.
@POST
@Path("/authenticate")
public boolean authenticate(@HeaderParam("Authorization") String atoken,
@DefaultValue("application/json") @HeaderParam("Content-Type") String contentType,
@DefaultValue("application/json") @HeaderParam("Accept") String accept,
String account) throws IOException {
final AtomicBoolean authenticated = new AtomicBoolean(false);
final AtomicBoolean done = new AtomicBoolean(false);
final AtomicReference uidRef = new AtomicReference<>();
FileInputStream serviceAccount = null;
try {
serviceAccount = new FileInputStream(FIREBASE_SNIPPET_PATH);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (FirebaseApp.getApps().isEmpty()) {
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredential(FirebaseCredentials.fromCertificate(serviceAccount))
.setDatabaseUrl("https://dojo-ibl.firebaseio.com")
.build();
FirebaseApp.initializeApp(options, "dojo-ibl");
}
FirebaseApp app = FirebaseApp.getInstance("dojo-ibl");
Task<FirebaseToken> task = FirebaseAuth.getInstance(app).verifyIdToken(atoken);
while (!task.isComplete()) {
try {
Thread.sleep(50);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
if (task.isSuccessful()) {
uidRef.set(task.getResult().getUid());
authenticated.set(true);
done.set(true);
}
else {
task.getException().printStackTrace();
authenticated.set(false);
done.set(true);
}
return authenticated.get();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment