Skip to content
This repository has been archived by the owner on Jan 24, 2019. It is now read-only.

Add Cloudfoundry provider #207

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
77 changes: 77 additions & 0 deletions providers/cloudfoundry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package providers

import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
)

type CloudFoundryProvider struct {
*ProviderData
}

func NewCloudFoundryProvider(p *ProviderData) *CloudFoundryProvider {
p.ProviderName = "CloudFoundry"
// Defaults for Bosh-Lite, must be configured for other OAuth Endpoints.
if p.LoginURL == nil || p.LoginURL.String() == "" {
p.LoginURL = &url.URL{
Scheme: "http",
Host: "login.bosh-lite.com",
Path: "/oauth/authorize",
}
}
if p.RedeemURL == nil || p.RedeemURL.String() == "" {
p.RedeemURL = &url.URL{
Scheme: "http",
Host: "login.bosh-lite.com",
Path: "/oauth/token",
}
}
if p.ValidateURL == nil || p.ValidateURL.String() == "" {
p.ValidateURL = &url.URL{
Scheme: "http",
Host: "login.bosh-lite.com",
Path: "/userinfo",
}
}
if p.Scope == "" {
p.Scope = "openid"
}
return &CloudFoundryProvider{ProviderData: p}
}

func (p *CloudFoundryProvider) GetEmailAddress(s *SessionState) (string, error) {

var email struct {
Email string `json:"email"`
}

params := url.Values{
"access_token": {s.AccessToken},
}
endpoint := p.ValidateURL.String() + "?" + params.Encode()
resp, err := http.DefaultClient.Get(endpoint)
if err != nil {
return "", err
}
body, err := ioutil.ReadAll(resp.Body)
resp.Body.Close()
if err != nil {
return "", err
}

if resp.StatusCode != 200 {
return "", fmt.Errorf("got %d from %q %s", resp.StatusCode, endpoint, body)
} else {
log.Printf("got %d from %q %s", resp.StatusCode, endpoint, body)
}

if err := json.Unmarshal(body, &email); err != nil {
return "", fmt.Errorf("%s unmarshaling %s", err, body)
}

return email.Email, nil
}
1 change: 0 additions & 1 deletion providers/provider_default.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ func (p *ProviderData) Redeem(redirectURL, code string) (s *SessionState, err er
return
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")

var resp *http.Response
resp, err = http.DefaultClient.Do(req)
if err != nil {
Expand Down
2 changes: 2 additions & 0 deletions providers/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ func New(provider string, p *ProviderData) Provider {
return NewAzureProvider(p)
case "gitlab":
return NewGitLabProvider(p)
case "cloudfoundry":
return NewCloudFoundryProvider(p)
default:
return NewGoogleProvider(p)
}
Expand Down