From abfbfbb52ee75ac7237bcc115b648c17ecad53d8 Mon Sep 17 00:00:00 2001 From: daniele Date: Sat, 2 Jan 2021 18:41:17 +0100 Subject: [PATCH] added stub to auth0 provider to connect to a custom oauth provider --- examples/docker-compose.yml | 55 ++++++++++ examples/nginx/localhost.crt | 24 +++++ examples/nginx/localhost.key | 28 +++++ examples/nginx/nginx.conf | 26 +++++ src/auth0/auth0.go | 201 +++++++++++++++++++++++++++++++++++ src/auth0/session.go | 65 +++++++++++ src/main.go | 3 +- 7 files changed, 401 insertions(+), 1 deletion(-) create mode 100644 examples/docker-compose.yml create mode 100644 examples/nginx/localhost.crt create mode 100644 examples/nginx/localhost.key create mode 100644 examples/nginx/nginx.conf create mode 100644 src/auth0/auth0.go create mode 100644 src/auth0/session.go diff --git a/examples/docker-compose.yml b/examples/docker-compose.yml new file mode 100644 index 0000000..a9d454e --- /dev/null +++ b/examples/docker-compose.yml @@ -0,0 +1,55 @@ +version: "3.7" + +services: + proxy: + image: nginx + volumes: + - ./nginx:/etc/nginx/ + ports: + # for https communication + - "443:80" + + minio: + image: minio/minio:RELEASE.2020-11-25T22-36-25Z + environment: + MINIO_ACCESS_KEY: test + MINIO_SECRET_KEY: testtest123 + command: server /data0 /data1 /data2 /data3 + + adminio-ui: + container_name: gui + image: rzrbld/adminio-ui:release-1.7 + environment: + # you will need to add admnio-api.example.com to you /etc/hosts file to emulate a live service + API_BASE_URL: "https://admnio-api.example.com" + ports: + - 80:80 + + # the name of this service is needed to set the correct cookie domain + # you will need to change the entry in the nginx.conf file if you want to use another domain + adminio.example.com: + container_name: api + image: adminio:local + build: + dockerfile: Dockerfile.dev + context: .. + environment: + MINIO_ACCESS: test + MINIO_SECRET: testtest123 + MINIO_HOST_PORT: minio:9000 + ADMINIO_HOST_PORT: 0.0.0.0:8080 + ADMINIO_OAUTH_ENABLE: "true" + # configuration to use your custom Oauth provider, located at auth.example.com + ADMINIO_OAUTH_PROVIDER: auth0 + ADMINIO_OAUTH_CUSTOM_DOMAIN: auth.example.com/oauth + MINIO_OAUTH_SERVER_DOMAIN: auth.example.com/oauth + ADMINIO_OAUTH_CALLBACK: https://adminio-api.example.com/auth/callback + ADMINIO_OAUTH_CLIENT_ID: your_id + ADMINIO_OAUTH_CLIENT_SECRET: your_secret + # variables used in file auth0/auth0.go + MINIO_OAUTH_TOKEN_PATH: "/token" + MINIO_OAUTH_AUTHORIZE_PATH: "/oauth/authorize" + MINIO_OAUTH_TOKEN_INFO_PATH: "/token/info" + ports: + # port exposed for http communication + - 90:8080 diff --git a/examples/nginx/localhost.crt b/examples/nginx/localhost.crt new file mode 100644 index 0000000..0f7b1ff --- /dev/null +++ b/examples/nginx/localhost.crt @@ -0,0 +1,24 @@ +-----BEGIN CERTIFICATE----- +MIIEDTCCAvWgAwIBAgIJAMJbEfOkofPkMA0GCSqGSIb3DQEBCwUAMIGcMQswCQYD +VQQGEwJERTEPMA0GA1UECBMGQmVybGluMQ8wDQYDVQQHEwZCZXJsaW4xFzAVBgNV +BAoTDmVuZXJnZW5vdXMgR2JSMRQwEgYDVQQLEwtlbmVyZ2VuaW91czEXMBUGA1UE +AxMOZW5lcmdlbmlvdXMuZXUxIzAhBgkqhkiG9w0BCQEWFGFkbWluQGVuZXJnZW5p +b3VzLmV1MB4XDTE5MDkyOTE4MjQxM1oXDTIwMDkyODE4MjQxM1owgZwxCzAJBgNV +BAYTAkRFMQ8wDQYDVQQIEwZCZXJsaW4xDzANBgNVBAcTBkJlcmxpbjEXMBUGA1UE +ChMOZW5lcmdlbm91cyBHYlIxFDASBgNVBAsTC2VuZXJnZW5pb3VzMRcwFQYDVQQD +Ew5lbmVyZ2VuaW91cy5ldTEjMCEGCSqGSIb3DQEJARYUYWRtaW5AZW5lcmdlbmlv +dXMuZXUwggEiMA0GCSqGSIb3DQEBAQUAA4IBDwAwggEKAoIBAQCs9GtXWq306m6V +PhryN0LcbDxRYLWIOoehFiAshduLvl2wcE55Js4iLxY5TS5S3Cu5t6u7cMXWWACd +I5HY3alKLd3QasrT7WIGMhEKy5nVJYaSYb88b9yGGoj+vB/d9eGqQavElGRKE9Cn +amocc+j2C2YRxZaYGBHs+B1Mz5oLvJrUOFl/pkFUizY+B64ssJTvOdISNk+jKLlh +JVgCX4yzi7LwUJV3NiMbQbUOPwHzjppH0FrIoMn99dRDcrBUi/cz53HZTa3P/F9Y +0hXk+3914fIICbtn4vUV29UJQkBifyww9xxmFZlMMJt+5wCiZGcQKH51tsY09t86 +QX2PC+fNAgMBAAGjUDBOMB0GA1UdDgQWBBRRv0upoPrZvXuVzJ4wnNOCTyHmVzAf +BgNVHSMEGDAWgBRRv0upoPrZvXuVzJ4wnNOCTyHmVzAMBgNVHRMEBTADAQH/MA0G +CSqGSIb3DQEBCwUAA4IBAQBesYfaPRpzmUkwv/zDyWraNqkjHL6PuhSAXnNVf+1V +jbKqzl3ksH/sHxsc43KZ/RT6Y4zOnwKsjQJ9bAqMAPdVwXtbmWkv6tIG4FN7cxoH +eR0RFDakGgsuafBHjhVp310c9glhiSAl6vQ8ffy4t4ycyRj7yemljoORhv5X5XsP +NY3T4XV7S01lAeBNikw1j1qMS3JSza6VoeP/rk5w5aveOGJDtc+Kj0K9FdjHAeEg +K68SJeHU9zeGHVJm8PcGk4i7GWVIUm2PpXO+14TXHhSMv4jNd2sn5mdATxa9vhHZ +Asj2/vCwtzzi9rzgVwOpCdvNaLNjot6fI4aGbzR6eJrL +-----END CERTIFICATE----- diff --git a/examples/nginx/localhost.key b/examples/nginx/localhost.key new file mode 100644 index 0000000..690585d --- /dev/null +++ b/examples/nginx/localhost.key @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvgIBADANBgkqhkiG9w0BAQEFAASCBKgwggSkAgEAAoIBAQCs9GtXWq306m6V +PhryN0LcbDxRYLWIOoehFiAshduLvl2wcE55Js4iLxY5TS5S3Cu5t6u7cMXWWACd +I5HY3alKLd3QasrT7WIGMhEKy5nVJYaSYb88b9yGGoj+vB/d9eGqQavElGRKE9Cn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mpAEbHBCowanMQKBgQCT +Uw7A4liyTWoZvxAthPw23orq4fqoNKknGKuyM1uPGZpFnPuxzrH3wE9Mb20+TqMT +olDcw7P0mD+/un5VihlrJ5IHRSwzZnrS6MmajXlJd2flde/SOYf1KfdNtxZK51dY +/3FYhyUq/x8K8TVPuP7TTXZyJms6oA/ogrfA61SwRQKBgGa0hOsKoMnfdxt99sGf +ZX1YY7UXuUmHBArNFoDd1krmX+315O3+dM8Dfqn+TRtlVMXHyq3dfAsUNqt0C9/q +XDhGUG6bKjBawVvACyZ494Bhut7i9nOD2CcIvNvJYHJWZ7kF452PpRZ6b3Me0T81 +EadZPQO3mBxk9BXLffQRuPVB +-----END PRIVATE KEY----- diff --git a/examples/nginx/nginx.conf b/examples/nginx/nginx.conf new file mode 100644 index 0000000..abc7c32 --- /dev/null +++ b/examples/nginx/nginx.conf @@ -0,0 +1,26 @@ +user nginx; + +events { + worker_connections 1024; +} + +worker_processes 1; + +http { + + keepalive_timeout 65; + + server { + listen 80 ssl; + server_name localhost; + + ssl_certificate /etc/nginx/localhost.crt; + ssl_certificate_key /etc/nginx/localhost.key; + + access_log /var/log/nginx/example.log; + + location / { + proxy_pass http://admnio.example.com:8080/; + } + } +} diff --git a/src/auth0/auth0.go b/src/auth0/auth0.go new file mode 100644 index 0000000..e5ab37b --- /dev/null +++ b/src/auth0/auth0.go @@ -0,0 +1,201 @@ +// taken from : https://github.com/markbates/goth/blob/master/providers/auth0/auth0.go +// Package auth0 implements the OAuth2 protocol for authenticating users through uber. +// This package can be used as a reference implementation of an OAuth2 provider for Goth. +package auth0 + +import ( + "bytes" + "encoding/json" + "io" + "fmt" + "net/http" + "github.com/markbates/goth" + "golang.org/x/oauth2" + // TODO: this function could be imported instead of being redefined + // getEnv "github.com/rzrbld/adminio-api/config" + "os" +) + +const ( + protocol string = "https://" +) + +var ( + tokenEndpoint = getEnv("MINIO_OAUTH_TOKEN_PATH", "/oauth/token") + authEndpoint = getEnv("MINIO_OAUTH_AUTHORIZE_PATH", "/authorize") + endpointProfile = getEnv("MINIO_OAUTH_TOKEN_INFO_PATH", "/userinfo") + customDomain = getEnv("MINIO_OAUTH_SERVER_DOMAIN", "auth0.com/oauth") +) + +func getEnv(key, fallback string) string { + value, exist := os.LookupEnv(key) + + if !exist { + return fallback + } + + return value +} + +// Provider is the implementation of `goth.Provider` for accessing Auth0. +type Provider struct { + ClientKey string + Secret string + CallbackURL string + Domain string + HTTPClient *http.Client + config *oauth2.Config + providerName string +} + +type auth0UserResp struct { + Name string `json:"name"` + NickName string `json:"nickname"` + Email string `json:"email"` + UserID string `json:"sub"` + AvatarURL string `json:"picture"` +} + +// New creates a new Auth0 provider and sets up important connection details. +// You should always call `auth0.New` to get a new provider. Never try to +// create one manually. +func New(clientKey, secret, callbackURL string, auth0Domain string, scopes ...string) *Provider { + p := &Provider{ + ClientKey: clientKey, + Secret: secret, + CallbackURL: callbackURL, + Domain: auth0Domain, + providerName: "auth0", + } + p.config = newConfig(p, scopes) + return p +} + +// Name is the name used to retrieve this provider later. +func (p *Provider) Name() string { + return p.providerName +} + +// SetName is to update the name of the provider (needed in case of multiple providers of 1 type) +func (p *Provider) SetName(name string) { + p.providerName = name +} + +func (p *Provider) Client() *http.Client { + return goth.HTTPClientWithFallBack(p.HTTPClient) +} + +// Debug is a no-op for the auth0 package. +func (p *Provider) Debug(debug bool) {} + +// BeginAuth asks Auth0 for an authentication end-point. +func (p *Provider) BeginAuth(state string) (goth.Session, error) { + return &Session{ + AuthURL: p.config.AuthCodeURL(state), + }, nil +} + +// FetchUser will go to Auth0 and access basic information about the user. +// the full response will be included in RawData +// https://auth0.com/docs/api/authentication#get-user-info + +func (p *Provider) FetchUser(session goth.Session) (goth.User, error) { + s := session.(*Session) + user := goth.User{ + AccessToken: s.AccessToken, + Provider: p.Name(), + RefreshToken: s.RefreshToken, + ExpiresAt: s.ExpiresAt, + } + + + if user.AccessToken == "" { + // data is not yet retrieved since accessToken is still empty + return user, fmt.Errorf("%s cannot get user information without accessToken", p.providerName) + } + + userProfileURL := protocol + customDomain + endpointProfile + req, err := http.NewRequest("GET", userProfileURL, nil) + if err != nil { + return user, err + } + req.Header.Set("Authorization", "Bearer "+s.AccessToken) + resp, err := p.Client().Do(req) + if err != nil { + if resp != nil { + resp.Body.Close() + } + return user, err + } + defer resp.Body.Close() + + if resp.StatusCode != http.StatusOK { + return user, fmt.Errorf("%s responded with a %d trying to fetch user information", p.providerName, resp.StatusCode) + } + + err = userFromReader(resp.Body, &user) + return user, err +} + +func newConfig(provider *Provider, scopes []string) *oauth2.Config { + c := &oauth2.Config{ + ClientID: provider.ClientKey, + ClientSecret: provider.Secret, + RedirectURL: provider.CallbackURL, + Endpoint: oauth2.Endpoint{ + AuthURL: protocol + customDomain + authEndpoint, + TokenURL: protocol + customDomain + tokenEndpoint, + }, + Scopes: []string{}, + } + + if len(scopes) > 0 { + for _, scope := range scopes { + c.Scopes = append(c.Scopes, scope) + } + } else { + c.Scopes = append(c.Scopes, "profile", "openid") + } + + return c +} + +func userFromReader(r io.Reader, user *goth.User) error { + var rawData map[string]interface{} + + buf := new(bytes.Buffer) + buf.ReadFrom(r) + err := json.Unmarshal(buf.Bytes(), &rawData) + if err != nil { + return err + } + + u := auth0UserResp{} + err = json.Unmarshal(buf.Bytes(), &u) + if err != nil { + return err + } + user.Email = u.Email + user.Name = u.Name + user.NickName = u.NickName + user.UserID = u.UserID + user.AvatarURL = u.AvatarURL + user.RawData = rawData + return nil +} + +//RefreshTokenAvailable refresh token is provided by auth provider or not +func (p *Provider) RefreshTokenAvailable() bool { + return true +} + +//RefreshToken get new access token based on the refresh token +func (p *Provider) RefreshToken(refreshToken string) (*oauth2.Token, error) { + token := &oauth2.Token{RefreshToken: refreshToken} + ts := p.config.TokenSource(oauth2.NoContext, token) + newToken, err := ts.Token() + if err != nil { + return nil, err + } + return newToken, err +} diff --git a/src/auth0/session.go b/src/auth0/session.go new file mode 100644 index 0000000..9dd70ca --- /dev/null +++ b/src/auth0/session.go @@ -0,0 +1,65 @@ +// taken from: https://raw.githubusercontent.com/markbates/goth/master/providers/auth0/session.go +package auth0 + +import ( + "encoding/json" + "errors" + "strings" + "time" + + "github.com/markbates/goth" + "golang.org/x/oauth2" +) + +// Session stores data during the auth process with Auth0. +type Session struct { + AuthURL string + AccessToken string + RefreshToken string + ExpiresAt time.Time +} + +var _ goth.Session = &Session{} + +// GetAuthURL will return the URL set by calling the `BeginAuth` function on the Auth0 provider. +func (s Session) GetAuthURL() (string, error) { + if s.AuthURL == "" { + return "", errors.New(goth.NoAuthUrlErrorMessage) + } + return s.AuthURL, nil +} + +// Authorize the session with Auth0 and return the access token to be stored for future use. +func (s *Session) Authorize(provider goth.Provider, params goth.Params) (string, error) { + p := provider.(*Provider) + token, err := p.config.Exchange(oauth2.NoContext, params.Get("code")) + if err != nil { + return "", err + } + + if !token.Valid() { + return "", errors.New("Invalid token received from provider") + } + + s.AccessToken = token.AccessToken + s.RefreshToken = token.RefreshToken + s.ExpiresAt = token.Expiry + return token.AccessToken, err +} + +// Marshal the session into a string +func (s Session) Marshal() string { + b, _ := json.Marshal(s) + return string(b) +} + +func (s Session) String() string { + return s.Marshal() +} + +// UnmarshalSession wil unmarshal a JSON string into a session. +func (p *Provider) UnmarshalSession(data string) (goth.Session, error) { + s := &Session{} + err := json.NewDecoder(strings.NewReader(data)).Decode(s) + return s, err +} diff --git a/src/main.go b/src/main.go index a6c8b89..c85edb6 100644 --- a/src/main.go +++ b/src/main.go @@ -8,7 +8,8 @@ import ( iris "github.com/kataras/iris/v12" "github.com/markbates/goth" "github.com/markbates/goth/providers/amazon" - "github.com/markbates/goth/providers/auth0" + // "github.com/markbates/goth/providers/auth0" + auth0 "github.com/rzrbld/adminio-api/auth0" "github.com/markbates/goth/providers/bitbucket" "github.com/markbates/goth/providers/box" "github.com/markbates/goth/providers/digitalocean"