Commit 5bb22b83 by Dan Cech Committed by Torkel Ödegaard

Generic Oauth Support for ADFS (#9242)

* check upn field if email address isn't present, support for adfs

* correctly set login to the user's email address if not specified by the oauth server

* break up GenericOAuth.UserInfo into helper functions
parent 1a019052
...@@ -5,6 +5,7 @@ import ( ...@@ -5,6 +5,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
"net/mail"
"github.com/grafana/grafana/pkg/models" "github.com/grafana/grafana/pkg/models"
...@@ -165,15 +166,18 @@ func (s *GenericOAuth) FetchOrganizations(client *http.Client) ([]string, error) ...@@ -165,15 +166,18 @@ func (s *GenericOAuth) FetchOrganizations(client *http.Client) ([]string, error)
return logins, nil return logins, nil
} }
type UserInfoJson struct {
Name string `json:"name"`
DisplayName string `json:"display_name"`
Login string `json:"login"`
Username string `json:"username"`
Email string `json:"email"`
Upn string `json:"upn"`
Attributes map[string][]string `json:"attributes"`
}
func (s *GenericOAuth) UserInfo(client *http.Client) (*BasicUserInfo, error) { func (s *GenericOAuth) UserInfo(client *http.Client) (*BasicUserInfo, error) {
var data struct { var data UserInfoJson
Name string `json:"name"`
DisplayName string `json:"display_name"`
Login string `json:"login"`
Username string `json:"username"`
Email string `json:"email"`
Attributes map[string][]string `json:"attributes"`
}
response, err := HttpGet(client, s.apiUrl) response, err := HttpGet(client, s.apiUrl)
if err != nil { if err != nil {
...@@ -185,33 +189,25 @@ func (s *GenericOAuth) UserInfo(client *http.Client) (*BasicUserInfo, error) { ...@@ -185,33 +189,25 @@ func (s *GenericOAuth) UserInfo(client *http.Client) (*BasicUserInfo, error) {
return nil, fmt.Errorf("Error getting user info: %s", err) return nil, fmt.Errorf("Error getting user info: %s", err)
} }
userInfo := &BasicUserInfo{ name, err := s.extractName(data)
Name: data.Name, if err != nil {
Login: data.Login, return nil, err
Email: data.Email,
}
if userInfo.Email == "" && data.Attributes["email:primary"] != nil {
userInfo.Email = data.Attributes["email:primary"][0]
}
if userInfo.Email == "" {
userInfo.Email, err = s.FetchPrivateEmail(client)
if err != nil {
return nil, err
}
} }
if userInfo.Name == "" && data.DisplayName != "" { email, err := s.extractEmail(data, client)
userInfo.Name = data.DisplayName if err != nil {
return nil, err
} }
if userInfo.Login == "" && data.Username != "" { login, err := s.extractLogin(data, email)
userInfo.Login = data.Username if err != nil {
return nil, err
} }
if userInfo.Login == "" { userInfo := &BasicUserInfo{
userInfo.Login = data.Email Name: name,
Login: login,
Email: email,
} }
if !s.IsTeamMember(client) { if !s.IsTeamMember(client) {
...@@ -224,3 +220,46 @@ func (s *GenericOAuth) UserInfo(client *http.Client) (*BasicUserInfo, error) { ...@@ -224,3 +220,46 @@ func (s *GenericOAuth) UserInfo(client *http.Client) (*BasicUserInfo, error) {
return userInfo, nil return userInfo, nil
} }
func (s *GenericOAuth) extractEmail(data UserInfoJson, client *http.Client) (string, error) {
if data.Email != "" {
return data.Email, nil
}
if data.Attributes["email:primary"] != nil {
return data.Attributes["email:primary"][0], nil
}
if data.Upn != "" {
emailAddr, emailErr := mail.ParseAddress(data.Upn)
if emailErr == nil {
return emailAddr.Address, nil
}
}
return s.FetchPrivateEmail(client)
}
func (s *GenericOAuth) extractLogin(data UserInfoJson, email string) (string, error) {
if data.Login != "" {
return data.Login, nil
}
if data.Username != "" {
return data.Username, nil
}
return email, nil
}
func (s *GenericOAuth) extractName(data UserInfoJson) (string, error) {
if data.Name != "" {
return data.Name, nil
}
if data.DisplayName != "" {
return data.DisplayName, nil
}
return "", nil
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment