Last active
April 15, 2018 12:07
-
-
Save kevintanhongann/737088b10bd4bbdab88fb59129f754eb to your computer and use it in GitHub Desktop.
FirebaseAuthService
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
import grails.gorm.transactions.Transactional | |
import groovy.json.JsonSlurper | |
import groovyx.net.http.HttpBuilder | |
import io.reactivex.Emitter | |
import io.reactivex.Observable | |
import org.springframework.http.HttpStatus | |
import static groovyx.net.http.HttpBuilder.configure | |
import groovyx.net.http.* | |
import static groovyx.net.http.ContentTypes.JSON | |
class FirebaseAuthService { | |
def login(String email, String password, String apiKey) { | |
def postBody = [ | |
email : email, | |
password: password | |
] | |
return Observable.create({ Emitter emitter -> | |
configure { | |
request.uri = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=' + apiKey | |
request.contentType = JSON[0] | |
}.postAsync(LoginResponse) { | |
request.body = postBody | |
response.parser('application/json'){ cc, fs -> | |
def json = new JsonSlurper().parse(fs.inputStream) | |
if(fs.statusCode == HttpStatus.OK.value() || fs.statusCode == HttpStatus.ACCEPTED.value()){ | |
def loginResponse = new LoginResponse(json) | |
emitter.onNext(loginResponse) | |
}else{ | |
def errorResponse = new FirebaseErrorResponse(json) | |
emitter.onError(errorResponse) | |
} | |
emitter.onComplete() | |
} | |
response.exception { t -> | |
emitter.onError(t) | |
emitter.onComplete() | |
} | |
} | |
}) | |
} | |
def oauthLogin(requestUri, String apiKey, String oauthPostBody, boolean returnSecureToken, boolean returnIdpCredential) { | |
def postBody = [ | |
requestUri : requestUri, | |
postBody : oauthPostBody, | |
returnSecureToken : returnSecureToken, | |
returnIdpCredential: returnIdpCredential | |
] | |
return Observable.create({ Emitter emitter -> | |
configure { | |
request.uri = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyAssertion?key=' + apiKey | |
request.contentType = JSON[0] | |
}.postAsync { | |
request.body = postBody | |
response.parser('application/json'){ cc, fs-> | |
def json = new JsonSlurper().parse(fs.inputStream) | |
if(fs.statusCode == HttpStatus.OK.value() || fs.statusCode == HttpStatus.ACCEPTED.value()){ | |
def response = new OauthLoginResponse(json) | |
emitter.onNext(response) | |
}else{ | |
def errorResponse = new FirebaseErrorResponse(json) | |
emitter.onError(errorResponse) | |
} | |
emitter.onComplete() | |
} | |
response.exception { t -> | |
emitter.onError(t) | |
emitter.onComplete() | |
} | |
} | |
}) | |
} | |
def linkWithOauth(String idToken, String requestUri, String oauthPostBody, String returnSecureToken, String returnIdpCredential, String apiKey) { | |
def postBody = [ | |
idToken : idToken, | |
requestUri : requestUri, | |
postBody : oauthPostBody, | |
returnSecureToken : returnSecureToken, | |
returnIdpCredential: returnIdpCredential | |
] | |
return Observable.create({ Emitter emitter -> | |
configure { | |
request.uri = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyAssertion?key=' + apiKey | |
request.contentType = JSON[0] | |
}.postAsync { | |
request.body = postBody | |
response.parser('application/json'){ cc, fs-> | |
def json = new JsonSlurper().parse(fs.inputStream) | |
if(fs.statusCode == HttpStatus.OK.value() || fs.statusCode == HttpStatus.ACCEPTED.value()){ | |
def linkWithOauthResponse = new LinkWithOauthResponse(json) | |
emitter.onNext(linkWithOauthResponse) | |
}else{ | |
def errorResponse = new FirebaseErrorResponse(json) | |
emitter.onError(errorResponse) | |
} | |
emitter.onComplete() | |
} | |
response.exception { t -> | |
emitter.onError(t) | |
emitter.onComplete() | |
} | |
} | |
}) | |
} | |
def register(String email, String password, String apiKey, boolean returnSecureToken) { | |
def postBody = [ | |
email : email, | |
password : password, | |
returnSecureToken: returnSecureToken | |
] | |
return Observable.create({ Emitter emitter -> | |
configure { | |
request.uri = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/signupNewUser?key=' + apiKey | |
request.contentType = JSON[0] | |
}.postAsync { | |
request.body = postBody | |
response.parser('application/json'){ cc, fs-> | |
def json = new JsonSlurper().parse(fs.inputStream) | |
if(fs.statusCode == HttpStatus.OK.value() || fs.statusCode == HttpStatus.ACCEPTED.value()){ | |
RegisterResponse registerResponse = new RegisterResponse(json) | |
emitter.onNext(registerResponse) | |
}else{ | |
def errorResponse = new FirebaseErrorResponse(json) | |
emitter.onError(errorResponse) | |
} | |
emitter.onComplete() | |
} | |
response.exception { t -> | |
emitter.onError(t) | |
emitter.onComplete() | |
} | |
} | |
}) | |
} | |
def sendEmailVerification(String idToken, String apiKey) { | |
def postBody = [ | |
requestType: 'VERIFY_EMAIL', | |
idToken : idToken | |
] | |
return Observable.create({ Emitter emitter -> | |
configure { | |
request.uri = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/getOobConfirmationCode?key=' + apiKey | |
request.contentType = JSON[0] | |
}.postAsync { | |
request.body = postBody | |
response.parser(JSON[0]) { cc, fs -> | |
def json = new JsonSlurper().parse(fs.inputStream) | |
if(fs.statusCode == HttpStatus.OK.value() || fs.statusCode == HttpStatus.ACCEPTED.value()){ | |
def emailVerificationResponse = new EmailVerificationResponse(json) | |
emitter.onNext(emailVerificationResponse) | |
}else{ | |
def errorResponse = new FirebaseErrorResponse(json) | |
emitter.onNext(errorResponse) | |
} | |
emitter.onComplete() | |
} | |
response.exception { t -> | |
emitter.onError(t) | |
emitter.onComplete() | |
} | |
} | |
}) | |
} | |
def confirmEmailVerification(String oobCode, String apiKey) { | |
def postBody = [ | |
oobCode: oobCode | |
] | |
return Observable.create({ Emitter emitter -> | |
configure { | |
request.uri = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/setAccountInfo?key=' + apiKey | |
request.contentType = JSON[0] | |
}.postAsync { | |
request.body = postBody | |
response.parser(JSON[0]) { cc, fs -> | |
def json = new JsonSlurper().parse(fs.inputStream) | |
if(fs.statusCode == HttpStatus.OK.value() || fs.statusCode == HttpStatus.ACCEPTED.value()){ | |
def confirmEmailVerificationResponse = new ConfirmEmailVerificationResponse(json) | |
emitter.onNext(confirmEmailVerificationResponse) | |
}else{ | |
def errorResponse = new FirebaseErrorResponse(json) | |
emitter.onNext(errorResponse) | |
} | |
emitter.onComplete() | |
} | |
response.exception { t -> | |
emitter.onError(t) | |
emitter.onComplete() | |
} | |
} | |
}) | |
} | |
def requestPassReset(String email, String apiKey) { | |
def postBody = [ | |
requestType: 'PASSWORD_RESET', | |
email : email | |
] | |
return Observable.create({ Emitter emitter -> | |
configure { | |
request.uri = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/getOobConfirmationCode?key=' + apiKey | |
request.contentType = JSON[0] | |
}.postAsync { | |
request.body = postBody | |
response.parser(JSON[0]) { cc, fs -> | |
def json = new JsonSlurper().parse(fs.inputStream) | |
if(fs.statusCode == HttpStatus.OK.value() || fs.statusCode == HttpStatus.ACCEPTED.value()){ | |
def requestPasswordResetResponse = new RequestPasswordResetResponse(json) | |
emitter.onNext(requestPasswordResetResponse) | |
}else { | |
def errorResponse = new FirebaseErrorResponse(json) | |
emitter.onError(errorResponse) | |
} | |
emitter.onComplete() | |
} | |
response.exception { t -> | |
t.printStackTrace() | |
emitter.onError(t) | |
emitter.onComplete() | |
} | |
} | |
}) | |
} | |
def changePassword(String idToken, String password, boolean returnSecureToken, String apiKey) { | |
def postBody = [ | |
idToken : idToken, | |
password : password, | |
returnSecureToken: returnSecureToken | |
] | |
return Observable.create({ Emitter emitter -> | |
configure { | |
request.uri = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/setAccountInfo?key=' + apiKey | |
request.contentType = JSON[0] | |
}.postAsync { | |
request.body = postBody | |
response.parser(JSON[0]) { cc, fs -> | |
def json = new JsonSlurper().parse(fs.inputStream) | |
if(fs.statusCode == HttpStatus.OK.value() || fs.statusCode == HttpStatus.ACCEPTED.value()) { | |
def changePasswordResponse = new ChangePasswordResponse(json) | |
emitter.onNext(changePasswordResponse) | |
}else{ | |
def errorResponse = new FirebaseErrorResponse(json) | |
emitter.onNext(errorResponse) | |
} | |
emitter.onComplete() | |
} | |
response.exception { t -> | |
emitter.onError(t) | |
emitter.onComplete() | |
} | |
} | |
}) | |
} | |
def resetPassword(String oobCode, String newPassword, String apiKey) { | |
def postBody = [ | |
oobCode : oobCode, | |
newPassword: newPassword | |
] | |
return Observable.create({ Emitter emitter -> | |
configure { | |
request.uri = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/resetPassword?key=' + apiKey | |
request.contentType = JSON[0] | |
}.postAsync { | |
request.body = postBody | |
response.parser(JSON[0]) { cc, fs -> | |
def json = new JsonSlurper().parse(fs.inputStream) | |
if(fs.statusCode == HttpStatus.OK.value() || fs.statusCode == HttpStatus.ACCEPTED.value()) { | |
def resetPasswordResponse = new ResetPasswordResponse(json) | |
emitter.onNext(resetPasswordResponse) | |
}else{ | |
def errorResponse = new FirebaseErrorResponse(json) | |
emitter.onNext(errorResponse) | |
} | |
emitter.onComplete() | |
} | |
response.exception { t -> | |
emitter.onError(t) | |
emitter.onComplete() | |
} | |
} | |
}) | |
} | |
def verifyPasswordReset(String oobCode, String apiKey) { | |
def postBody = [ | |
oobCode: oobCode | |
] | |
return Observable.create({ Emitter emitter -> | |
configure { | |
request.uri = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/resetPassword?key=' + apiKey | |
request.contentType = JSON[0] | |
}.postAsync { | |
request.body = postBody | |
response.parser(JSON[0]) { cc, fs -> | |
def json = new JsonSlurper().parse(fs.inputStream) | |
if(fs.statusCode == HttpStatus.OK.value() || fs.statusCode == HttpStatus.ACCEPTED.value()){ | |
def verifyPasswordResetResponse = new VerifyPasswordResetResponse(json) | |
emitter.onNext(verifyPasswordResetResponse) | |
}else{ | |
def errorResponse = new FirebaseErrorResponse(json) | |
emitter.onNext(errorResponse) | |
} | |
emitter.onComplete() | |
} | |
response.exception { t -> | |
emitter.onError(t) | |
emitter.onComplete() | |
} | |
} | |
}) | |
} | |
def getAccountInfo(String apiKey, String token) { | |
def postBody = [idToken: token] // will be url-encoded | |
return Observable.create({ Emitter emitter -> | |
configure { | |
request.uri = 'https://www.googleapis.com/identitytoolkit/v3/relyingparty/getAccountInfo?key=' + apiKey | |
request.contentType = JSON[0] | |
}.postAsync { | |
request.body = postBody | |
response.parser(JSON[0]) { cc, fs -> | |
def json = new JsonSlurper().parse(fs.inputStream) | |
if(fs.statusCode == HttpStatus.OK.value() || fs.statusCode == HttpStatus.ACCEPTED.value()) { | |
def accountInfoResponse = new AccountInfoResponse(json) | |
emitter.onNext(accountInfoResponse) | |
}else { | |
def errorResponse = new FirebaseErrorResponse(json) | |
emitter.onNext(errorResponse) | |
} | |
emitter.onComplete() | |
} | |
response.exception { t -> | |
emitter.onError(t) | |
emitter.onComplete() | |
} | |
} | |
}) | |
} | |
def logout() { | |
//todo clear out the cookies and redirect | |
} | |
def exchangeForToken(String refreshToken, String apiKey) { | |
def postBody = [ | |
grantType : 'refresh_token', | |
refresh_token: refreshToken | |
] | |
return Observable.create({ Emitter emitter -> | |
configure { | |
request.uri = 'https://securetoken.googleapis.com/v1/token?key=' + apiKey | |
request.contentType = JSON[0] | |
}.postAsync { | |
request.body = postBody | |
response.parser(JSON[0]) { cc, fs -> | |
def json = new JsonSlurper().parse(fs.inputStream) | |
if(fs.statusCode == HttpStatus.OK.value() || fs.statusCode == HttpStatus.ACCEPTED.value()) { | |
ExchangeForTokenResponse exchangeForTokenResponse = new ExchangeForTokenResponse(json) | |
emitter.onNext(exchangeForTokenResponse) | |
}else{ | |
def errorResponse = new FirebaseErrorResponse(json) | |
emitter.onNext(errorResponse) | |
} | |
emitter.onComplete() | |
} | |
response.exception { t -> | |
emitter.onError(t) | |
emitter.onComplete() | |
} | |
} | |
}) | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment