2015-03-31 21:17:17 +02:00
|
|
|
package providers
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2015-05-21 08:50:21 +02:00
|
|
|
"github.com/bitly/oauth2_proxy/api"
|
2015-03-31 21:17:17 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type MyUsaProvider struct {
|
|
|
|
*ProviderData
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewMyUsaProvider(p *ProviderData) *MyUsaProvider {
|
|
|
|
const myUsaHost string = "alpha.my.usa.gov"
|
|
|
|
|
|
|
|
p.ProviderName = "MyUSA"
|
2015-11-09 01:47:44 +02:00
|
|
|
if p.LoginURL.String() == "" {
|
|
|
|
p.LoginURL = &url.URL{Scheme: "https",
|
2015-03-31 21:17:17 +02:00
|
|
|
Host: myUsaHost,
|
|
|
|
Path: "/oauth/authorize"}
|
|
|
|
}
|
2015-11-09 01:47:44 +02:00
|
|
|
if p.RedeemURL.String() == "" {
|
|
|
|
p.RedeemURL = &url.URL{Scheme: "https",
|
2015-03-31 21:17:17 +02:00
|
|
|
Host: myUsaHost,
|
|
|
|
Path: "/oauth/token"}
|
|
|
|
}
|
2015-11-09 01:47:44 +02:00
|
|
|
if p.ProfileURL.String() == "" {
|
|
|
|
p.ProfileURL = &url.URL{Scheme: "https",
|
2015-03-31 21:17:17 +02:00
|
|
|
Host: myUsaHost,
|
|
|
|
Path: "/api/v1/profile"}
|
|
|
|
}
|
2015-11-09 01:47:44 +02:00
|
|
|
if p.ValidateURL.String() == "" {
|
|
|
|
p.ValidateURL = &url.URL{Scheme: "https",
|
2015-05-08 23:13:35 +02:00
|
|
|
Host: myUsaHost,
|
|
|
|
Path: "/api/v1/tokeninfo"}
|
|
|
|
}
|
2015-03-31 21:17:17 +02:00
|
|
|
if p.Scope == "" {
|
|
|
|
p.Scope = "profile.email"
|
|
|
|
}
|
|
|
|
return &MyUsaProvider{ProviderData: p}
|
|
|
|
}
|
|
|
|
|
2015-06-23 13:23:39 +02:00
|
|
|
func (p *MyUsaProvider) GetEmailAddress(s *SessionState) (string, error) {
|
2015-03-31 21:17:17 +02:00
|
|
|
req, err := http.NewRequest("GET",
|
2015-11-09 01:47:44 +02:00
|
|
|
p.ProfileURL.String()+"?access_token="+s.AccessToken, nil)
|
2015-03-31 21:17:17 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed building request %s", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
json, err := api.Request(req)
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("failed making request %s", err)
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return json.Get("email").String()
|
|
|
|
}
|