support oidc identity provider
Signed-off-by: hongming <talonwan@yunify.com>
This commit is contained in:
@@ -18,21 +18,25 @@ package github
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"encoding/json"
|
||||
"github.com/mitchellh/mapstructure"
|
||||
"golang.org/x/oauth2"
|
||||
"io/ioutil"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/authentication/identityprovider"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/authentication/oauth"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
const (
|
||||
UserInfoURL = "https://api.github.com/user"
|
||||
userInfoURL = "https://api.github.com/user"
|
||||
authURL = "https://github.com/login/oauth/authorize"
|
||||
tokenURL = "https://github.com/login/oauth/access_token"
|
||||
)
|
||||
|
||||
func init() {
|
||||
identityprovider.RegisterOAuthProvider(&githubProviderFactory{})
|
||||
identityprovider.RegisterOAuthProvider(&ldapProviderFactory{})
|
||||
}
|
||||
|
||||
type github struct {
|
||||
@@ -52,15 +56,21 @@ type github struct {
|
||||
// the OAuth flow, after the resource owner's URLs.
|
||||
RedirectURL string `json:"redirectURL" yaml:"redirectURL"`
|
||||
|
||||
// Used to turn off TLS certificate checks
|
||||
InsecureSkipVerify bool `json:"insecureSkipVerify" yaml:"insecureSkipVerify"`
|
||||
|
||||
// Scope specifies optional requested permissions.
|
||||
Scopes []string `json:"scopes" yaml:"scopes"`
|
||||
|
||||
Config *oauth2.Config `json:"-" yaml:"-"`
|
||||
}
|
||||
|
||||
// endpoint represents an OAuth 2.0 provider's authorization and token
|
||||
// endpoint URLs.
|
||||
type endpoint struct {
|
||||
AuthURL string `json:"authURL" yaml:"authURL"`
|
||||
TokenURL string `json:"tokenURL" yaml:"tokenURL"`
|
||||
AuthURL string `json:"authURL" yaml:"authURL"`
|
||||
TokenURL string `json:"tokenURL" yaml:"tokenURL"`
|
||||
UserInfoURL string `json:"userInfoURL" yaml:"userInfoURL"`
|
||||
}
|
||||
|
||||
type githubIdentity struct {
|
||||
@@ -102,18 +112,44 @@ type githubIdentity struct {
|
||||
Collaborators int `json:"collaborators"`
|
||||
}
|
||||
|
||||
type githubProviderFactory struct {
|
||||
type ldapProviderFactory struct {
|
||||
}
|
||||
|
||||
func (g *githubProviderFactory) Type() string {
|
||||
func (g *ldapProviderFactory) Type() string {
|
||||
return "GitHubIdentityProvider"
|
||||
}
|
||||
|
||||
func (g *githubProviderFactory) Create(options *oauth.DynamicOptions) (identityprovider.OAuthProvider, error) {
|
||||
func (g *ldapProviderFactory) Create(options oauth.DynamicOptions) (identityprovider.OAuthProvider, error) {
|
||||
var github github
|
||||
if err := mapstructure.Decode(options, &github); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if github.Endpoint.AuthURL == "" {
|
||||
github.Endpoint.AuthURL = authURL
|
||||
}
|
||||
if github.Endpoint.TokenURL == "" {
|
||||
github.Endpoint.TokenURL = tokenURL
|
||||
}
|
||||
if github.Endpoint.UserInfoURL == "" {
|
||||
github.Endpoint.UserInfoURL = userInfoURL
|
||||
}
|
||||
// fixed options
|
||||
options["endpoint"] = oauth.DynamicOptions{
|
||||
"authURL": github.Endpoint.AuthURL,
|
||||
"tokenURL": github.Endpoint.TokenURL,
|
||||
"userInfoURL": github.Endpoint.UserInfoURL,
|
||||
}
|
||||
github.Config = &oauth2.Config{
|
||||
ClientID: github.ClientID,
|
||||
ClientSecret: github.ClientSecret,
|
||||
Endpoint: oauth2.Endpoint{
|
||||
AuthURL: github.Endpoint.AuthURL,
|
||||
TokenURL: github.Endpoint.TokenURL,
|
||||
},
|
||||
RedirectURL: github.RedirectURL,
|
||||
Scopes: github.Scopes,
|
||||
}
|
||||
return &github, nil
|
||||
}
|
||||
|
||||
@@ -129,29 +165,23 @@ func (g githubIdentity) GetEmail() string {
|
||||
return g.Email
|
||||
}
|
||||
|
||||
func (g githubIdentity) GetDisplayName() string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func (g *github) IdentityExchange(code string) (identityprovider.Identity, error) {
|
||||
config := oauth2.Config{
|
||||
ClientID: g.ClientID,
|
||||
ClientSecret: g.ClientSecret,
|
||||
Endpoint: oauth2.Endpoint{
|
||||
AuthURL: g.Endpoint.AuthURL,
|
||||
TokenURL: g.Endpoint.TokenURL,
|
||||
AuthStyle: oauth2.AuthStyleAutoDetect,
|
||||
},
|
||||
RedirectURL: g.RedirectURL,
|
||||
Scopes: g.Scopes,
|
||||
ctx := context.TODO()
|
||||
if g.InsecureSkipVerify {
|
||||
client := &http.Client{
|
||||
Transport: &http.Transport{
|
||||
TLSClientConfig: &tls.Config{
|
||||
InsecureSkipVerify: true,
|
||||
},
|
||||
},
|
||||
}
|
||||
ctx = context.WithValue(ctx, oauth2.HTTPClient, client)
|
||||
}
|
||||
|
||||
token, err := config.Exchange(context.Background(), code)
|
||||
token, err := g.Config.Exchange(ctx, code)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
resp, err := oauth2.NewClient(context.Background(), oauth2.StaticTokenSource(token)).Get(UserInfoURL)
|
||||
resp, err := oauth2.NewClient(ctx, oauth2.StaticTokenSource(token)).Get(g.Endpoint.UserInfoURL)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
|
||||
Copyright 2020 The KubeSphere Authors.
|
||||
|
||||
Licensed under the Apache License, Version 2.0 (the "License");
|
||||
you may not use this file except in compliance with the License.
|
||||
You may obtain a copy of the License at
|
||||
|
||||
http://www.apache.org/licenses/LICENSE-2.0
|
||||
|
||||
Unless required by applicable law or agreed to in writing, software
|
||||
distributed under the License is distributed on an "AS IS" BASIS,
|
||||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
*/
|
||||
|
||||
package github
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
. "github.com/onsi/ginkgo"
|
||||
. "github.com/onsi/gomega"
|
||||
"github.com/onsi/gomega/gexec"
|
||||
"golang.org/x/oauth2"
|
||||
"gopkg.in/yaml.v3"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/authentication/identityprovider"
|
||||
"kubesphere.io/kubesphere/pkg/apiserver/authentication/oauth"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var githubServer *httptest.Server
|
||||
|
||||
func TestGithub(t *testing.T) {
|
||||
RegisterFailHandler(Fail)
|
||||
RunSpecs(t, "GitHub Identity Provider Suite")
|
||||
}
|
||||
|
||||
var _ = BeforeSuite(func(done Done) {
|
||||
githubServer = httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
var data map[string]interface{}
|
||||
switch r.RequestURI {
|
||||
case "/login/oauth/access_token":
|
||||
data = map[string]interface{}{
|
||||
"access_token": "e72e16c7e42f292c6912e7710c838347ae178b4a",
|
||||
"scope": "user,repo,gist",
|
||||
"token_type": "bearer",
|
||||
}
|
||||
case "/user":
|
||||
data = map[string]interface{}{
|
||||
"login": "test",
|
||||
"email": "test@kubesphere.io",
|
||||
}
|
||||
default:
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
w.Write([]byte("not implemented"))
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Add("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(data)
|
||||
}))
|
||||
close(done)
|
||||
}, 60)
|
||||
|
||||
var _ = AfterSuite(func() {
|
||||
By("tearing down the test environment")
|
||||
gexec.KillAndWait(5 * time.Second)
|
||||
githubServer.Close()
|
||||
})
|
||||
|
||||
var _ = Describe("GitHub", func() {
|
||||
Context("GitHub", func() {
|
||||
var (
|
||||
provider identityprovider.OAuthProvider
|
||||
err error
|
||||
)
|
||||
It("should configure successfully", func() {
|
||||
configYAML := `
|
||||
clientID: de6ff8bed0304e487b6e
|
||||
clientSecret: 2b70536f79ec8d2939863509d05e2a71c268b9af
|
||||
redirectURL: "http://ks-console/oauth/redirect"
|
||||
scopes:
|
||||
- user
|
||||
`
|
||||
config := mustUnmarshalYAML(configYAML)
|
||||
factory := ldapProviderFactory{}
|
||||
provider, err = factory.Create(config)
|
||||
Expect(err).Should(BeNil())
|
||||
expected := &github{
|
||||
ClientID: "de6ff8bed0304e487b6e",
|
||||
ClientSecret: "2b70536f79ec8d2939863509d05e2a71c268b9af",
|
||||
Endpoint: endpoint{
|
||||
AuthURL: authURL,
|
||||
TokenURL: tokenURL,
|
||||
UserInfoURL: userInfoURL,
|
||||
},
|
||||
RedirectURL: "http://ks-console/oauth/redirect",
|
||||
Scopes: []string{"user"},
|
||||
Config: &oauth2.Config{
|
||||
ClientID: "de6ff8bed0304e487b6e",
|
||||
ClientSecret: "2b70536f79ec8d2939863509d05e2a71c268b9af",
|
||||
Endpoint: oauth2.Endpoint{
|
||||
AuthURL: authURL,
|
||||
TokenURL: tokenURL,
|
||||
},
|
||||
RedirectURL: "http://ks-console/oauth/redirect",
|
||||
Scopes: []string{"user"},
|
||||
},
|
||||
}
|
||||
Expect(provider).Should(Equal(expected))
|
||||
})
|
||||
It("should configure successfully", func() {
|
||||
config := oauth.DynamicOptions{
|
||||
"clientID": "de6ff8bed0304e487b6e",
|
||||
"clientSecret": "2b70536f79ec8d2939863509d05e2a71c268b9af",
|
||||
"redirectURL": "http://ks-console/oauth/redirect",
|
||||
"insecureSkipVerify": true,
|
||||
"endpoint": oauth.DynamicOptions{
|
||||
"authURL": fmt.Sprintf("%s/login/oauth/authorize", githubServer.URL),
|
||||
"tokenURL": fmt.Sprintf("%s/login/oauth/access_token", githubServer.URL),
|
||||
"userInfoURL": fmt.Sprintf("%s/user", githubServer.URL),
|
||||
},
|
||||
}
|
||||
factory := ldapProviderFactory{}
|
||||
provider, err = factory.Create(config)
|
||||
Expect(err).Should(BeNil())
|
||||
expected := oauth.DynamicOptions{
|
||||
"clientID": "de6ff8bed0304e487b6e",
|
||||
"clientSecret": "2b70536f79ec8d2939863509d05e2a71c268b9af",
|
||||
"redirectURL": "http://ks-console/oauth/redirect",
|
||||
"insecureSkipVerify": true,
|
||||
"endpoint": oauth.DynamicOptions{
|
||||
"authURL": fmt.Sprintf("%s/login/oauth/authorize", githubServer.URL),
|
||||
"tokenURL": fmt.Sprintf("%s/login/oauth/access_token", githubServer.URL),
|
||||
"userInfoURL": fmt.Sprintf("%s/user", githubServer.URL),
|
||||
},
|
||||
}
|
||||
Expect(config).Should(Equal(expected))
|
||||
})
|
||||
It("should login successfully", func() {
|
||||
identity, err := provider.IdentityExchange("3389")
|
||||
Expect(err).Should(BeNil())
|
||||
Expect(identity.GetUserID()).Should(Equal("test"))
|
||||
Expect(identity.GetUsername()).Should(Equal("test"))
|
||||
Expect(identity.GetEmail()).Should(Equal("test@kubesphere.io"))
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
func mustUnmarshalYAML(data string) oauth.DynamicOptions {
|
||||
var dynamicOptions oauth.DynamicOptions
|
||||
_ = yaml.Unmarshal([]byte(data), &dynamicOptions)
|
||||
return dynamicOptions
|
||||
}
|
||||
Reference in New Issue
Block a user