REGISTRATION_(FAILED|REFUSED)

This commit is contained in:
p1gp1g
2021-01-03 19:31:41 +01:00
parent 253ce0e751
commit e191b0d268
3 changed files with 57 additions and 27 deletions

View File

@@ -1,14 +1,22 @@
package com.github.gotify.service
/**
* Constants found here:
* https://github.com/UnifiedPush/UP-lib/blob/main/src/main/java/org/unifiedpush/connector/Constants.kt
* Constants as defined on the specs
* https://github.com/UnifiedPush/UP-spec/blob/main/specifications.md
*/
const val NEW_ENDPOINT = "org.unifiedpush.android.connector.NEW_ENDPOINT"
const val UNREGISTERED = "org.unifiedpush.android.connector.UNREGISTERED"
const val MESSAGE = "org.unifiedpush.android.connector.MESSAGE"
const val ACTION_NEW_ENDPOINT = "org.unifiedpush.android.connector.NEW_ENDPOINT"
const val ACTION_REGISTRATION_FAILED = "org.unifiedpush.android.connector.REGISTRATION_FAILED"
const val ACTION_REGISTRATION_REFUSED = "org.unifiedpush.android.connector.REGISTRATION_REFUSED"
const val ACTION_UNREGISTERED = "org.unifiedpush.android.connector.UNREGISTERED"
const val ACTION_MESSAGE = "org.unifiedpush.android.connector.MESSAGE"
const val REGISTER = "org.unifiedpush.android.distributor.REGISTER"
const val UNREGISTER = "org.unifiedpush.android.distributor.UNREGISTER"
const val MESSAGE_ACK = "org.unifiedpush.android.distributor.MESSAGE_ACK"
const val ACTION_REGISTER = "org.unifiedpush.android.distributor.REGISTER"
const val ACTION_UNREGISTER = "org.unifiedpush.android.distributor.UNREGISTER"
const val ACTION_MESSAGE_ACK = "org.unifiedpush.android.distributor.MESSAGE_ACK"
const val EXTRA_APPLICATION = "application"
const val EXTRA_TOKEN = "token"
const val EXTRA_ENDPOINT = "endpoint"
const val EXTRA_MESSAGE = "message"
const val EXTRA_MESSAGE_ID = "id"

View File

@@ -13,9 +13,9 @@ fun sendMessage(context: Context, application: String, message: String){
val token = getToken(context,application)!!
val broadcastIntent = Intent()
broadcastIntent.`package` = application
broadcastIntent.action = MESSAGE
broadcastIntent.putExtra("token", token)
broadcastIntent.putExtra("message", message)
broadcastIntent.action = ACTION_MESSAGE
broadcastIntent.putExtra(EXTRA_TOKEN, token)
broadcastIntent.putExtra(EXTRA_MESSAGE, message)
context.sendBroadcast(broadcastIntent)
}
@@ -23,18 +23,36 @@ fun sendEndpoint(context: Context, application: String, endpoint: String) {
val token = getToken(context,application)!!
val broadcastIntent = Intent()
broadcastIntent.`package` = application
broadcastIntent.action = NEW_ENDPOINT
broadcastIntent.putExtra("token", token)
broadcastIntent.putExtra("endpoint", endpoint)
broadcastIntent.action = ACTION_NEW_ENDPOINT
broadcastIntent.putExtra(EXTRA_TOKEN, token)
broadcastIntent.putExtra(EXTRA_ENDPOINT, endpoint)
context.sendBroadcast(broadcastIntent)
}
fun sendUnregistered(context: Context, application: String, _token: String?){
val token = _token?: getToken(context,application)!!
val token = _token?: getToken(context, application)!!
val broadcastIntent = Intent()
broadcastIntent.`package` = application
broadcastIntent.action = UNREGISTERED
broadcastIntent.putExtra("token", token)
broadcastIntent.action = ACTION_UNREGISTERED
broadcastIntent.putExtra(EXTRA_TOKEN, token)
context.sendBroadcast(broadcastIntent)
}
fun sendRegistrationFailed(context: Context, application: String, token: String, message: String){
val broadcastIntent = Intent()
broadcastIntent.`package` = application
broadcastIntent.action = ACTION_REGISTRATION_FAILED
broadcastIntent.putExtra(EXTRA_TOKEN, token)
broadcastIntent.putExtra(EXTRA_MESSAGE, message)
context.sendBroadcast(broadcastIntent)
}
fun sendRegistrationRefused(context: Context, application: String, token: String, message: String) {
val broadcastIntent = Intent()
broadcastIntent.`package` = application
broadcastIntent.action = ACTION_REGISTRATION_REFUSED
broadcastIntent.putExtra(EXTRA_TOKEN, token)
broadcastIntent.putExtra(EXTRA_MESSAGE, message)
context.sendBroadcast(broadcastIntent)
}

View File

@@ -30,7 +30,7 @@ class RegisterBroadcastReceiver: BroadcastReceiver() {
}
}
private fun registerApp(db: MessagingDatabase, application: String, connector_token: String) {
private fun registerApp(context: Context?, db: MessagingDatabase, application: String, connector_token: String) {
if (application.isBlank()) {
Log.w("RegisterService","Trying to register an app without packageName")
return
@@ -46,12 +46,16 @@ class RegisterBroadcastReceiver: BroadcastReceiver() {
// User should unregister this app manually
// to avoid an app to impersonate another one
if (db.isRegistered(application)) {
Log.w("RegisterService","$application already registered with a different token")
val message = "$application already registered with a different token"
Log.w("RegisterService",message)
sendRegistrationRefused(context!!,application,connector_token,message)
return
}
val app = createApp(application)
if(app == null){
Log.w("RegisterService","Cannot create a new app to register")
val message = "Cannot create a new app to register"
Log.w("RegisterService", message)
sendRegistrationFailed(context!!,application,connector_token,message)
return
}
db.registerApp(application, app.id, app.token, connector_token)
@@ -86,13 +90,13 @@ class RegisterBroadcastReceiver: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
settings = Settings(context)
when (intent!!.action) {
REGISTER ->{
ACTION_REGISTER ->{
val db = MessagingDatabase(context!!)
Log.i("Register","REGISTER")
val internalToken = intent.getStringExtra("token")?: ""
val application = intent.getStringExtra("application")?: ""
val internalToken = intent.getStringExtra(EXTRA_TOKEN)?: ""
val application = intent.getStringExtra(EXTRA_APPLICATION)?: ""
thread(start = true) {
registerApp(db, application, internalToken)
registerApp(context, db, application, internalToken)
Log.i("RegisterService","Registration is finished")
}.join()
val token = db.getGotifyToken(application, false)
@@ -101,10 +105,10 @@ class RegisterBroadcastReceiver: BroadcastReceiver() {
sendEndpoint(context,application,endpoint)
db.close()
}
UNREGISTER ->{
ACTION_UNREGISTER ->{
Log.i("Register","UNREGISTER")
val token = intent.getStringExtra("token")?: ""
val application = intent.getStringExtra("application")?: ""
val token = intent.getStringExtra(EXTRA_TOKEN)?: ""
val application = intent.getStringExtra(EXTRA_APPLICATION)?: ""
thread(start = true) {
val db = MessagingDatabase(context!!)
unregisterApp(db,application, token)