Skip to content

HueAuthApi: allow token validation #64

@mudar

Description

@mudar

Proposed Change:

Thanks Renee for this great library 😃

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"}}]

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions