Sample of token based authorization


(Michael Parment) #1

I am planning to create a binding for Husqvarna AutoMover as my second binding.
I will look at https://github.com/chrisz/pyhusmow
It seems that it uses Token based authorization.
Is there any other binding that uses this where i can look.
I am still struggling with learning Java :slight_smile:

/Mike


(Hilbrand Bouwkamp) #2

Do you know which protocol? OAuth2? or something custom?


(Michael Parment) #3

Seems in pyton that you post username and password and receive token informattion.

    def login(self, login, password):
        response = self.session.post(self._API_IM + 'token',
                                     headers=self._HEADERS,
                                     json={
                                         "data": {
                                             "attributes": {
                                                 "password": password,
                                                 "username": login
                                             },
                                             "type": "token"
                                         }
                                     })

        response.raise_for_status()
        self.logger.info('Logged in successfully')

        json = response.json()
        self.set_token(json["data"]["id"], json["data"]["attributes"]["provider"])
        return json["data"]["attributes"]["expires_in"]

/mike


(Hilbrand Bouwkamp) #4

I donโ€™t know what protocol that is. It looks custom and simple. There is probably no standard support out-of-the-box for it. So you need to implement something yourself. Using HttpUtil you should be able to get the token and store it in some cache. For some inspiration look this earlier Spotify api implementation. Itโ€™s based on OAuth2. The earlier implementation had OAuth2 implemented, but current PR uses the OAuth2 implementation part of openHAB. To give you an example here is the token cache handling:

Just browser there to other files too and copy what you can use. Hope this helps somewhat.


(Michael Parment) #5

Thanks now i have somewhere to start.

/Mike