2018-11-26 20:21:03 +05:30
|
|
|
// Package keystone provides authentication strategy using Keystone.
|
|
|
|
package keystone
|
|
|
|
|
|
|
|
import (
|
2018-12-13 16:52:53 +05:30
|
|
|
"bytes"
|
2018-11-26 20:21:03 +05:30
|
|
|
"context"
|
|
|
|
"encoding/json"
|
2018-12-13 16:52:53 +05:30
|
|
|
"fmt"
|
2018-11-26 20:21:03 +05:30
|
|
|
"io/ioutil"
|
2018-12-13 16:52:53 +05:30
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/dexidp/dex/connector"
|
2019-02-22 17:49:23 +05:30
|
|
|
"github.com/dexidp/dex/pkg/log"
|
2018-11-26 20:21:03 +05:30
|
|
|
)
|
|
|
|
|
2018-12-20 21:55:22 +05:30
|
|
|
type conn struct {
|
|
|
|
Domain string
|
|
|
|
Host string
|
|
|
|
AdminUsername string
|
|
|
|
AdminPassword string
|
2019-02-22 17:49:23 +05:30
|
|
|
Logger log.Logger
|
2018-12-20 21:55:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
type userKeystone struct {
|
|
|
|
Domain domainKeystone `json:"domain"`
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type domainKeystone struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Config holds the configuration parameters for Keystone connector.
|
|
|
|
// Keystone should expose API v3
|
|
|
|
// An example config:
|
|
|
|
// connectors:
|
|
|
|
// type: keystone
|
|
|
|
// id: keystone
|
|
|
|
// name: Keystone
|
|
|
|
// config:
|
|
|
|
// keystoneHost: http://example:5000
|
|
|
|
// domain: default
|
|
|
|
// keystoneUsername: demo
|
|
|
|
// keystonePassword: DEMO_PASS
|
|
|
|
type Config struct {
|
|
|
|
Domain string `json:"domain"`
|
|
|
|
Host string `json:"keystoneHost"`
|
|
|
|
AdminUsername string `json:"keystoneUsername"`
|
|
|
|
AdminPassword string `json:"keystonePassword"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type loginRequestData struct {
|
|
|
|
auth `json:"auth"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type auth struct {
|
|
|
|
Identity identity `json:"identity"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type identity struct {
|
|
|
|
Methods []string `json:"methods"`
|
|
|
|
Password password `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type password struct {
|
|
|
|
User user `json:"user"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type user struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Domain domain `json:"domain"`
|
|
|
|
Password string `json:"password"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type domain struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type token struct {
|
|
|
|
User userKeystone `json:"user"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type tokenResponse struct {
|
|
|
|
Token token `json:"token"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type group struct {
|
|
|
|
ID string `json:"id"`
|
|
|
|
Name string `json:"name"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type groupsResponse struct {
|
|
|
|
Groups []group `json:"groups"`
|
|
|
|
}
|
|
|
|
|
2018-11-26 20:21:03 +05:30
|
|
|
var (
|
2018-12-20 21:55:22 +05:30
|
|
|
_ connector.PasswordConnector = &conn{}
|
|
|
|
_ connector.RefreshConnector = &conn{}
|
2018-11-26 20:21:03 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// Open returns an authentication strategy using Keystone.
|
2019-02-22 17:49:23 +05:30
|
|
|
func (c *Config) Open(id string, logger log.Logger) (connector.Connector, error) {
|
2018-12-20 21:55:22 +05:30
|
|
|
return &conn{
|
|
|
|
c.Domain,
|
|
|
|
c.Host,
|
|
|
|
c.AdminUsername,
|
|
|
|
c.AdminPassword,
|
|
|
|
logger}, nil
|
2018-11-26 20:21:03 +05:30
|
|
|
}
|
|
|
|
|
2018-12-20 21:55:22 +05:30
|
|
|
func (p *conn) Close() error { return nil }
|
2018-11-26 20:21:03 +05:30
|
|
|
|
2018-12-20 21:55:22 +05:30
|
|
|
func (p *conn) Login(ctx context.Context, scopes connector.Scopes, username, password string) (identity connector.Identity, validPassword bool, err error) {
|
2018-12-13 16:52:53 +05:30
|
|
|
resp, err := p.getTokenResponse(ctx, username, password)
|
|
|
|
if err != nil {
|
|
|
|
return identity, false, fmt.Errorf("keystone: error %v", err)
|
|
|
|
}
|
2018-12-20 21:55:22 +05:30
|
|
|
if resp.StatusCode/100 != 2 {
|
|
|
|
return identity, false, fmt.Errorf("keystone login: error %v", resp.StatusCode)
|
|
|
|
}
|
|
|
|
if resp.StatusCode != 201 {
|
|
|
|
return identity, false, nil
|
|
|
|
}
|
|
|
|
token := resp.Header.Get("X-Subject-Token")
|
|
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return identity, false, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
var tokenResp = new(tokenResponse)
|
|
|
|
err = json.Unmarshal(data, &tokenResp)
|
|
|
|
if err != nil {
|
|
|
|
return identity, false, fmt.Errorf("keystone: invalid token response: %v", err)
|
|
|
|
}
|
|
|
|
if scopes.Groups {
|
2018-12-13 16:52:53 +05:30
|
|
|
groups, err := p.getUserGroups(ctx, tokenResp.Token.User.ID, token)
|
|
|
|
if err != nil {
|
|
|
|
return identity, false, err
|
|
|
|
}
|
|
|
|
identity.Groups = groups
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|
2018-12-20 21:55:22 +05:30
|
|
|
identity.Username = username
|
|
|
|
identity.UserID = tokenResp.Token.User.ID
|
|
|
|
return identity, true, nil
|
2018-11-26 20:21:03 +05:30
|
|
|
}
|
|
|
|
|
2018-12-20 21:55:22 +05:30
|
|
|
func (p *conn) Prompt() string { return "username" }
|
2018-11-27 15:58:46 +05:30
|
|
|
|
2018-12-20 21:55:22 +05:30
|
|
|
func (p *conn) Refresh(
|
|
|
|
ctx context.Context, scopes connector.Scopes, identity connector.Identity) (connector.Identity, error) {
|
2018-12-13 16:52:53 +05:30
|
|
|
token, err := p.getAdminToken(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return identity, fmt.Errorf("keystone: failed to obtain admin token: %v", err)
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|
2018-12-13 16:52:53 +05:30
|
|
|
ok, err := p.checkIfUserExists(ctx, identity.UserID, token)
|
|
|
|
if err != nil {
|
|
|
|
return identity, err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
return identity, fmt.Errorf("keystone: user %q does not exist", identity.UserID)
|
|
|
|
}
|
2018-12-20 21:55:22 +05:30
|
|
|
if scopes.Groups {
|
|
|
|
groups, err := p.getUserGroups(ctx, identity.UserID, token)
|
|
|
|
if err != nil {
|
|
|
|
return identity, err
|
|
|
|
}
|
|
|
|
identity.Groups = groups
|
2018-12-13 16:52:53 +05:30
|
|
|
}
|
2018-11-27 15:58:46 +05:30
|
|
|
return identity, nil
|
2018-11-26 20:21:03 +05:30
|
|
|
}
|
|
|
|
|
2018-12-20 21:55:22 +05:30
|
|
|
func (p *conn) getTokenResponse(ctx context.Context, username, pass string) (response *http.Response, err error) {
|
2018-12-13 16:52:53 +05:30
|
|
|
client := &http.Client{}
|
|
|
|
jsonData := loginRequestData{
|
|
|
|
auth: auth{
|
|
|
|
Identity: identity{
|
|
|
|
Methods: []string{"password"},
|
|
|
|
Password: password{
|
|
|
|
User: user{
|
|
|
|
Name: username,
|
|
|
|
Domain: domain{ID: p.Domain},
|
|
|
|
Password: pass,
|
2018-11-26 20:21:03 +05:30
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2018-12-13 16:52:53 +05:30
|
|
|
jsonValue, err := json.Marshal(jsonData)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-20 21:55:22 +05:30
|
|
|
// https://developer.openstack.org/api-ref/identity/v3/#password-authentication-with-unscoped-authorization
|
|
|
|
authTokenURL := p.Host + "/v3/auth/tokens/"
|
2018-12-13 16:52:53 +05:30
|
|
|
req, err := http.NewRequest("POST", authTokenURL, bytes.NewBuffer(jsonValue))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("Content-Type", "application/json")
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
|
|
|
|
return client.Do(req)
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|
2018-11-26 20:21:03 +05:30
|
|
|
|
2018-12-20 21:55:22 +05:30
|
|
|
func (p *conn) getAdminToken(ctx context.Context) (string, error) {
|
|
|
|
resp, err := p.getTokenResponse(ctx, p.AdminUsername, p.AdminPassword)
|
2018-12-13 16:52:53 +05:30
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-12-18 20:34:03 +05:30
|
|
|
defer resp.Body.Close()
|
|
|
|
|
2018-12-13 16:52:53 +05:30
|
|
|
token := resp.Header.Get("X-Subject-Token")
|
|
|
|
return token, nil
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|
2018-11-26 20:21:03 +05:30
|
|
|
|
2018-12-20 21:55:22 +05:30
|
|
|
func (p *conn) checkIfUserExists(ctx context.Context, userID string, token string) (bool, error) {
|
|
|
|
// https://developer.openstack.org/api-ref/identity/v3/#show-user-details
|
|
|
|
userURL := p.Host + "/v3/users/" + userID
|
2018-12-13 16:52:53 +05:30
|
|
|
client := &http.Client{}
|
|
|
|
req, err := http.NewRequest("GET", userURL, nil)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req.Header.Set("X-Auth-Token", token)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
2019-12-18 20:34:03 +05:30
|
|
|
defer resp.Body.Close()
|
2018-12-13 16:52:53 +05:30
|
|
|
|
|
|
|
if resp.StatusCode == 200 {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
return false, err
|
2018-11-26 20:21:03 +05:30
|
|
|
}
|
|
|
|
|
2018-12-20 21:55:22 +05:30
|
|
|
func (p *conn) getUserGroups(ctx context.Context, userID string, token string) ([]string, error) {
|
2018-12-13 16:52:53 +05:30
|
|
|
client := &http.Client{}
|
2018-12-20 21:55:22 +05:30
|
|
|
// https://developer.openstack.org/api-ref/identity/v3/#list-groups-to-which-a-user-belongs
|
|
|
|
groupsURL := p.Host + "/v3/users/" + userID + "/groups"
|
2018-12-13 16:52:53 +05:30
|
|
|
req, err := http.NewRequest("GET", groupsURL, nil)
|
2019-07-30 14:38:57 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-12-13 16:52:53 +05:30
|
|
|
req.Header.Set("X-Auth-Token", token)
|
|
|
|
req = req.WithContext(ctx)
|
|
|
|
resp, err := client.Do(req)
|
|
|
|
if err != nil {
|
|
|
|
p.Logger.Errorf("keystone: error while fetching user %q groups\n", userID)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
var groupsResp = new(groupsResponse)
|
|
|
|
|
|
|
|
err = json.Unmarshal(data, &groupsResp)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
groups := make([]string, len(groupsResp.Groups))
|
|
|
|
for i, group := range groupsResp.Groups {
|
|
|
|
groups[i] = group.Name
|
|
|
|
}
|
|
|
|
return groups, nil
|
2018-11-27 15:58:46 +05:30
|
|
|
}
|