Skip to content

Commit

Permalink
Token base auth (#9)
Browse files Browse the repository at this point in the history
* Support token based authentication

* Update docs
  • Loading branch information
mawalu authored Oct 1, 2019
1 parent f8ab57a commit a78c4e9
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 3 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ let ha = new Homeassistant({
protocol: 'ws', // "ws" (default) or "wss" for SSL
retryTimeout: 1000, // in ms, default is 5000
retryCount: 3, // default is 10, values < 0 mean unlimited
password: 'super_secure',
password: 'http_api_password', // api_password is getting depricated by home assistant
token: 'access_token' // for now both tokens and api_passwords are suported
port: 8123
})

Expand Down
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const defaultConfig = {
timeout: 5000,
retryCount: 10,
port: 8123,
password: ''
password: '',
token: ''
}

class Homeassistant extends EventEmitter {
Expand All @@ -35,7 +36,10 @@ class Homeassistant extends EventEmitter {
}

if (data.type == 'auth_required') {
if (!this.config.password) throw new Error('Password required')
if (!this.config.password && !this.config.token) throw new Error('Password required')

if (this.config.token)
return this.send({type: 'auth', access_token: this.config.token}, false)

return this.send({type: 'auth', api_password: this.config.password}, false)
}
Expand Down

0 comments on commit a78c4e9

Please sign in to comment.