Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

user's name retrieval for home assistant #566

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add new HA user info api
ivanjx authored Jun 29, 2024
commit 4fddf6595e53063c2e8db482535ba8a8e627afc9
1 change: 1 addition & 0 deletions config/config.yml_example_homeassistant
Original file line number Diff line number Diff line change
@@ -35,3 +35,4 @@ oauth:
callback_url: https://vouch.yourdomain.com/auth
auth_url: https://homeassistant.yourdomain.com:port/auth/authorize
token_url: https://homeassistant.yourdomain.com:port/auth/token
user_info_url: https://homeassistant.yourdomain.com:port/api/user
23 changes: 20 additions & 3 deletions pkg/providers/homeassistant/homeassistant.go
Original file line number Diff line number Diff line change
@@ -11,7 +11,9 @@ OR CONDITIONS OF ANY KIND, either express or implied.
package homeassistant

import (
"encoding/json"
"golang.org/x/oauth2"
"io/ioutil"
"net/http"

"github.com/vouch/vouch-proxy/pkg/cfg"
@@ -33,12 +35,27 @@ func (Provider) Configure() {
// GetUserInfo provider specific call to get userinfomation
// More info: https://developers.home-assistant.io/docs/en/auth_api.html
func (Provider) GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens, opts ...oauth2.AuthCodeOption) (rerr error) {
_, providerToken, err := common.PrepareTokensAndClient(r, ptokens, false, opts...)
client, providerToken, err := common.PrepareTokensAndClient(r, ptokens, false, opts...)
if err != nil {
return err
}
ptokens.PAccessToken = providerToken.Extra("access_token").(string)
// Home assistant does not provide an API to query username, so we statically set it to "homeassistant"
user.Username = "homeassistant"
userinfo, err := client.Get(cfg.GenOAuth.UserInfoURL)
if err != nil {
return err
}
defer func() {
if err := userinfo.Body.Close(); err != nil {
rerr = err
}
}()
data, _ := ioutil.ReadAll(userinfo.Body)
log.Infof("HA userinfo body: %s", string(data))
haUser := structs.User{}
if err = json.Unmarshal(data, &haUser); err != nil {
log.Error(err)
return err
}
user.Username = haUser.Username
return nil
}