forked from bitly/oauth2_proxy
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Kirill Melnikov
committed
May 12, 2017
1 parent
120a47a
commit 2c9d46f
Showing
22 changed files
with
236 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
FROM golang:onbuild-alpine |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
.PHONY: build | ||
|
||
clean: | ||
rm ./oauth2_proxy | ||
docker-compose down | ||
|
||
default: build | ||
|
||
build: | ||
@go build -a . | ||
|
||
run: clean build | ||
./oauth2_proxy -provider=passport -config etc/oauth2_proxy.cfg | ||
|
||
docker: | ||
docker-compose build | ||
|
||
up: docker | ||
docker-compose up -d |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
version: '2' | ||
|
||
services: | ||
|
||
nginx: | ||
image: nginx | ||
networks: | ||
oauth: | ||
ipv4_address: 172.15.66.10 | ||
|
||
proxy: | ||
build: . | ||
image: melnikk/oauth2_proxy | ||
volumes: | ||
- "./etc/oauth2_proxy.cfg:/etc/oauth2_proxy.cfg" | ||
command: /etc/oauth2_proxy.cfg | ||
networks: | ||
oauth: | ||
ipv4_address: 172.15.66.50 | ||
|
||
networks: | ||
oauth: | ||
driver: bridge | ||
driver_opts: | ||
com.docker.network.enable_ipv6: "false" | ||
ipam: | ||
driver: default | ||
config: | ||
- subnet: 172.15.66.0/24 | ||
gateway: 172.15.66.1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
package providers | ||
|
||
import ( | ||
"fmt" | ||
"encoding/json" | ||
"encoding/base64" | ||
"net/url" | ||
"bytes" | ||
"io/ioutil" | ||
"errors" | ||
"net/http" | ||
"github.com/dgrijalva/jwt-go" | ||
"log" | ||
"strings" | ||
) | ||
|
||
// PassportProvider of auth | ||
type PassportProvider struct { | ||
*ProviderData | ||
} | ||
|
||
// NewPassportProvider creates passport provider | ||
func NewPassportProvider(p *ProviderData) *PassportProvider { | ||
p.ProviderName = "Passport" | ||
|
||
return &PassportProvider{ProviderData: p} | ||
} | ||
|
||
|
||
func (p *PassportProvider) Redeem(redirectURL, code string) (s *SessionState, err error) { | ||
if code == "" { | ||
err = errors.New("missing code") | ||
return | ||
} | ||
|
||
params := url.Values{} | ||
params.Add("redirect_uri", redirectURL) | ||
params.Add("client_id", p.ClientID) | ||
params.Add("client_secret", p.ClientSecret) | ||
params.Add("code", code) | ||
params.Add("grant_type", "authorization_code") | ||
if p.ProtectedResource != nil && p.ProtectedResource.String() != "" { | ||
params.Add("resource", p.ProtectedResource.String()) | ||
} | ||
|
||
var req *http.Request | ||
req, err = http.NewRequest("POST", p.RedeemURL.String(), bytes.NewBufferString(params.Encode())) | ||
if err != nil { | ||
return | ||
} | ||
|
||
req.Header.Set("Accept", "application/json") | ||
req.Header.Set("Content-Type", "application/x-www-form-urlencoded") | ||
|
||
token := []byte(fmt.Sprintf("%s:%s", p.ClientID, p.ClientSecret)) | ||
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", base64.StdEncoding.EncodeToString(token))) | ||
|
||
var resp *http.Response | ||
resp, err = http.DefaultClient.Do(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
var body []byte | ||
body, err = ioutil.ReadAll(resp.Body) | ||
resp.Body.Close() | ||
if err != nil { | ||
return | ||
} | ||
|
||
if resp.StatusCode != 200 { | ||
err = fmt.Errorf("got %d from %q %s", resp.StatusCode, p.RedeemURL.String(), body) | ||
return | ||
} | ||
|
||
// blindly try json and x-www-form-urlencoded | ||
var jsonResponse struct { | ||
AccessToken string `json:"access_token"` | ||
} | ||
err = json.Unmarshal(body, &jsonResponse) | ||
|
||
if err == nil { | ||
|
||
if err != nil { | ||
return | ||
} | ||
s = &SessionState{ | ||
AccessToken: jsonResponse.AccessToken, | ||
} | ||
return | ||
} | ||
|
||
var v url.Values | ||
v, err = url.ParseQuery(string(body)) | ||
if err != nil { | ||
return | ||
} | ||
if a := v.Get("access_token"); a != "" { | ||
s = &SessionState{AccessToken: a} | ||
} else { | ||
err = fmt.Errorf("no access token found %s", body) | ||
} | ||
return | ||
} | ||
|
||
func (p *PassportProvider) GetEmailAddress(s *SessionState) (string, error) { | ||
email := "" | ||
token, err := jwt.Parse(s.AccessToken, func(token *jwt.Token) (interface{}, error) { | ||
publicKey, err := ioutil.ReadFile("etc/passport.pub") | ||
if err != nil { | ||
log.Printf("Error loading public key: %s", err.Error()) | ||
} | ||
return publicKey, nil | ||
}) | ||
if err==nil && token.Valid { | ||
login := strings.ToLower(token.Claims["sub"].(string)) | ||
|
||
loginParts := strings.Split(login,"\\") | ||
email = fmt.Sprintf("%s@%s", loginParts[1], loginParts[0]) | ||
} | ||
return email, err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.