Shade icon indicating copy to clipboard operation
Shade copied to clipboard

HueAuthApi: allow token validation

Open mudar opened this issue 3 years ago • 0 comments

Proposed Change:

Thanks Renee for this great library :smiley:

While migrating my app from the deprecated Philips sdk to Shade, I needed a callback to validate the token stored in the app's sharedPrefs. This was my temporary solution: I test the authentication by calling the groups endpoint, An invalid token would throw UnauthorizedException. The reason for calling the groups endpoint is because the payload was 2 kb vs 7 kb for the lights.

suspend fun Shade.isConnected(): Boolean = try {
    // Call the api to validate token, regardless of the result object
    groups.getGroups()
    true
} catch (e: UnauthorizedException) {
    false
}

If you think this should be added to Shade, my suggestion for a cleaner solution would be to call a non-existing endpoint and return a boolean based on the error type: Ex: /api/<username>/connected a valid token would return

[{"error":{"type":4,"address":"/connected","description":"method, GET, not available for resource, /connected"}}]

while a wrong token would return [{"error":{"type":1,"address":"/","description":"unauthorized user"}}]

mudar avatar Jan 22 '21 16:01 mudar