git.iko.soy/typeable/oauth2-proxy
on v2.2 help sign in
58 lines 1.3 kB history raw
1package providers
3import (
4 "log"
5 "net/http"
6 "net/url"
8 "github.com/bitly/oauth2_proxy/api"
9)
11type MyUsaProvider struct {
12 *ProviderData
15func NewMyUsaProvider(p *ProviderData) *MyUsaProvider {
16 const myUsaHost string = "alpha.my.usa.gov"
18 p.ProviderName = "MyUSA"
19 if p.LoginURL.String() == "" {
20 p.LoginURL = &url.URL{Scheme: "https",
21 Host: myUsaHost,
22 Path: "/oauth/authorize"}
23 }
24 if p.RedeemURL.String() == "" {
25 p.RedeemURL = &url.URL{Scheme: "https",
26 Host: myUsaHost,
27 Path: "/oauth/token"}
28 }
29 if p.ProfileURL.String() == "" {
30 p.ProfileURL = &url.URL{Scheme: "https",
31 Host: myUsaHost,
32 Path: "/api/v1/profile"}
33 }
34 if p.ValidateURL.String() == "" {
35 p.ValidateURL = &url.URL{Scheme: "https",
36 Host: myUsaHost,
37 Path: "/api/v1/tokeninfo"}
38 }
39 if p.Scope == "" {
40 p.Scope = "profile.email"
41 }
42 return &MyUsaProvider{ProviderData: p}
45func (p *MyUsaProvider) GetEmailAddress(s *SessionState) (string, error) {
46 req, err := http.NewRequest("GET",
47 p.ProfileURL.String()+"?access_token="+s.AccessToken, nil)
48 if err != nil {
49 log.Printf("failed building request %s", err)
50 return "", err
51 }
52 json, err := api.Request(req)
53 if err != nil {
54 log.Printf("failed making request %s", err)
55 return "", err
56 }
57 return json.Get("email").String()