Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
水淹萌龙
kubesphere
提交
34dfc204
K
kubesphere
项目概览
水淹萌龙
/
kubesphere
与 Fork 源项目一致
Fork自
KubeSphere / kubesphere
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
K
kubesphere
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
未验证
提交
34dfc204
编写于
3月 30, 2020
作者:
H
hongming
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add default oauth client
Signed-off-by:
N
hongming
<
talonwan@yunify.com
>
上级
4746c728
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
150 addition
and
223 deletion
+150
-223
pkg/apiserver/authentication/identityprovider/github/github.go
...piserver/authentication/identityprovider/github/github.go
+0
-198
pkg/apiserver/authentication/oauth/oauth_options.go
pkg/apiserver/authentication/oauth/oauth_options.go
+45
-2
pkg/apiserver/authentication/oauth/oauth_options_test.go
pkg/apiserver/authentication/oauth/oauth_options_test.go
+104
-0
pkg/apiserver/config/config_test.go
pkg/apiserver/config/config_test.go
+1
-23
未找到文件。
pkg/apiserver/authentication/identityprovider/github/github.go
已删除
100644 → 0
浏览文件 @
4746c728
/*
*
* 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
(
"context"
"encoding/json"
"golang.org/x/oauth2"
"gopkg.in/yaml.v2"
"io/ioutil"
"k8s.io/apiserver/pkg/authentication/user"
"kubesphere.io/kubesphere/pkg/apiserver/authentication/identityprovider"
"kubesphere.io/kubesphere/pkg/apiserver/authentication/oauth"
"time"
)
const
(
UserInfoURL
=
"https://api.github.com/user"
)
type
Github
struct
{
// ClientID is the application's ID.
ClientID
string
`json:"clientID" yaml:"clientID"`
// ClientSecret is the application's secret.
ClientSecret
string
`json:"-" yaml:"clientSecret"`
// Endpoint contains the resource server's token endpoint
// URLs. These are constants specific to each server and are
// often available via site-specific packages, such as
// google.Endpoint or github.Endpoint.
Endpoint
Endpoint
`json:"endpoint" yaml:"endpoint"`
// RedirectURL is the URL to redirect users going through
// the OAuth flow, after the resource owner's URLs.
RedirectURL
string
`json:"redirectURL" yaml:"redirectURL"`
// Scope specifies optional requested permissions.
Scopes
[]
string
`json:"scopes" yaml:"scopes"`
}
// 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"`
}
type
GithubIdentity
struct
{
Login
string
`json:"login"`
ID
string
`json:"id"`
NodeID
string
`json:"node_id"`
AvatarURL
string
`json:"avatar_url"`
GravatarID
string
`json:"gravatar_id"`
URL
string
`json:"url"`
HTMLURL
string
`json:"html_url"`
FollowersURL
string
`json:"followers_url"`
FollowingURL
string
`json:"following_url"`
GistsURL
string
`json:"gists_url"`
StarredURL
string
`json:"starred_url"`
SubscriptionsURL
string
`json:"subscriptions_url"`
OrganizationsURL
string
`json:"organizations_url"`
ReposURL
string
`json:"repos_url"`
EventsURL
string
`json:"events_url"`
ReceivedEventsURL
string
`json:"received_events_url"`
Type
string
`json:"type"`
SiteAdmin
bool
`json:"site_admin"`
Name
string
`json:"name"`
Company
string
`json:"company"`
Blog
string
`json:"blog"`
Location
string
`json:"location"`
Email
string
`json:"email"`
Hireable
bool
`json:"hireable"`
Bio
string
`json:"bio"`
PublicRepos
int
`json:"public_repos"`
PublicGists
int
`json:"public_gists"`
Followers
int
`json:"followers"`
Following
int
`json:"following"`
CreatedAt
time
.
Time
`json:"created_at"`
UpdatedAt
time
.
Time
`json:"updated_at"`
PrivateGists
int
`json:"private_gists"`
TotalPrivateRepos
int
`json:"total_private_repos"`
OwnedPrivateRepos
int
`json:"owned_private_repos"`
DiskUsage
int
`json:"disk_usage"`
Collaborators
int
`json:"collaborators"`
}
func
init
()
{
identityprovider
.
RegisterOAuthProviderCodec
(
&
codec
{
t
:
"github"
})
}
type
codec
struct
{
t
string
}
func
(
c
codec
)
Type
()
string
{
return
c
.
t
}
func
(
codec
)
Encode
(
provider
identityprovider
.
OAuthProvider
)
(
*
oauth
.
DynamicOptions
,
error
)
{
data
,
err
:=
yaml
.
Marshal
(
provider
)
if
err
!=
nil
{
return
nil
,
err
}
var
options
oauth
.
DynamicOptions
err
=
yaml
.
Unmarshal
(
data
,
&
options
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
options
,
nil
}
func
(
codec
)
Decode
(
options
*
oauth
.
DynamicOptions
)
(
identityprovider
.
OAuthProvider
,
error
)
{
data
,
err
:=
yaml
.
Marshal
(
options
)
if
err
!=
nil
{
return
nil
,
err
}
var
provider
Github
err
=
yaml
.
Unmarshal
(
data
,
&
provider
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
provider
,
nil
}
func
(
g
GithubIdentity
)
GetName
()
string
{
return
g
.
Login
}
func
(
g
GithubIdentity
)
GetUID
()
string
{
return
g
.
ID
}
func
(
g
GithubIdentity
)
GetGroups
()
[]
string
{
return
nil
}
func
(
g
GithubIdentity
)
GetExtra
()
map
[
string
][]
string
{
return
nil
}
func
(
g
*
Github
)
IdentityExchange
(
code
string
)
(
user
.
Info
,
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
,
}
token
,
err
:=
config
.
Exchange
(
context
.
Background
(),
code
)
if
err
!=
nil
{
return
nil
,
err
}
resp
,
err
:=
oauth2
.
NewClient
(
context
.
Background
(),
oauth2
.
StaticTokenSource
(
token
))
.
Get
(
UserInfoURL
)
if
err
!=
nil
{
return
nil
,
err
}
data
,
err
:=
ioutil
.
ReadAll
(
resp
.
Body
)
resp
.
Body
.
Close
()
if
err
!=
nil
{
return
nil
,
err
}
var
githubIdentity
GithubIdentity
err
=
json
.
Unmarshal
(
data
,
&
githubIdentity
)
if
err
!=
nil
{
return
nil
,
err
}
return
githubIdentity
,
nil
}
pkg/apiserver/authentication/oauth/oauth_options.go
浏览文件 @
34dfc204
...
...
@@ -21,6 +21,7 @@ package oauth
import
(
"errors"
"kubesphere.io/kubesphere/pkg/utils/sliceutil"
"net/url"
"time"
)
...
...
@@ -152,12 +153,33 @@ type Client struct {
AccessTokenInactivityTimeout
*
time
.
Duration
`json:"accessTokenInactivityTimeout,omitempty" yaml:"accessTokenInactivityTimeout,omitempty"`
}
var
(
// Allow any redirect URI if the redirectURI is defined in request
AllowAllRedirectURI
=
"*"
DefaultTokenMaxAge
=
time
.
Second
*
86400
DefaultAccessTokenInactivityTimeout
=
time
.
Duration
(
0
)
DefaultClients
=
[]
Client
{{
Name
:
"default"
,
RespondWithChallenges
:
true
,
RedirectURIs
:
[]
string
{
AllowAllRedirectURI
},
GrantMethod
:
GrantHandlerAuto
,
ScopeRestrictions
:
[]
string
{
"full"
},
AccessTokenMaxAge
:
&
DefaultTokenMaxAge
,
AccessTokenInactivityTimeout
:
&
DefaultAccessTokenInactivityTimeout
,
}}
)
func
(
o
*
Options
)
OAuthClient
(
name
string
)
(
Client
,
error
)
{
for
_
,
found
:=
range
o
.
Clients
{
if
found
.
Name
==
name
{
return
found
,
nil
}
}
for
_
,
defaultClient
:=
range
DefaultClients
{
if
defaultClient
.
Name
==
name
{
return
defaultClient
,
nil
}
}
return
Client
{},
ErrorClientNotFound
}
func
(
o
*
Options
)
IdentityProviderOptions
(
name
string
)
(
IdentityProviderOptions
,
error
)
{
...
...
@@ -169,16 +191,37 @@ func (o *Options) IdentityProviderOptions(name string) (IdentityProviderOptions,
return
IdentityProviderOptions
{},
ErrorClientNotFound
}
func
(
c
Client
)
anyRedirectAbleURI
()
[]
string
{
uris
:=
make
([]
string
,
0
)
for
_
,
uri
:=
range
c
.
RedirectURIs
{
_
,
err
:=
url
.
Parse
(
uri
)
if
err
==
nil
{
uris
=
append
(
uris
,
uri
)
}
}
return
uris
}
func
(
c
Client
)
ResolveRedirectURL
(
expectURL
string
)
(
string
,
error
)
{
// RedirectURIs is empty
if
len
(
c
.
RedirectURIs
)
==
0
{
return
""
,
ErrorRedirectURLNotAllowed
}
allowAllRedirectURI
:=
sliceutil
.
HasString
(
c
.
RedirectURIs
,
AllowAllRedirectURI
)
redirectAbleURIs
:=
c
.
anyRedirectAbleURI
()
if
expectURL
==
""
{
return
c
.
RedirectURIs
[
0
],
nil
// Need to specify at least one RedirectURI
if
len
(
redirectAbleURIs
)
>
0
{
return
redirectAbleURIs
[
0
],
nil
}
else
{
return
""
,
ErrorRedirectURLNotAllowed
}
}
if
sliceutil
.
HasString
(
c
.
Redirect
URIs
,
expectURL
)
{
if
allowAllRedirectURI
||
sliceutil
.
HasString
(
redirectAble
URIs
,
expectURL
)
{
return
expectURL
,
nil
}
return
""
,
ErrorRedirectURLNotAllowed
}
...
...
pkg/apiserver/authentication/oauth/oauth_options_test.go
0 → 100644
浏览文件 @
34dfc204
/*
*
* 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
oauth
import
(
"github.com/google/go-cmp/cmp"
"testing"
"time"
)
func
TestDefaultAuthOptions
(
t
*
testing
.
T
)
{
oneDay
:=
time
.
Second
*
86400
zero
:=
time
.
Duration
(
0
)
expect
:=
Client
{
Name
:
"default"
,
RespondWithChallenges
:
true
,
RedirectURIs
:
[]
string
{
AllowAllRedirectURI
},
GrantMethod
:
GrantHandlerAuto
,
ScopeRestrictions
:
[]
string
{
"full"
},
AccessTokenMaxAge
:
&
oneDay
,
AccessTokenInactivityTimeout
:
&
zero
,
}
options
:=
NewOptions
()
client
,
err
:=
options
.
OAuthClient
(
"default"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
diff
:=
cmp
.
Diff
(
expect
,
client
);
len
(
diff
)
!=
0
{
t
.
Errorf
(
"%T differ (-got, +expected), %s"
,
expect
,
diff
)
}
}
func
TestClientResolveRedirectURL
(
t
*
testing
.
T
)
{
options
:=
NewOptions
()
defaultClient
,
err
:=
options
.
OAuthClient
(
"default"
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
tests
:=
[]
struct
{
Name
string
client
Client
expectError
error
expectURL
string
}{
{
Name
:
"default client test"
,
client
:
defaultClient
,
expectError
:
nil
,
expectURL
:
"https://localhost:8080/auth/cb"
,
},
{
Name
:
"custom client test"
,
client
:
Client
{
Name
:
"default"
,
RespondWithChallenges
:
true
,
RedirectURIs
:
[]
string
{
"https://foo.bar.com/oauth/cb"
},
GrantMethod
:
GrantHandlerAuto
,
ScopeRestrictions
:
[]
string
{
"full"
},
},
expectError
:
ErrorRedirectURLNotAllowed
,
expectURL
:
"https://foo.bar.com/oauth/err"
,
},
{
Name
:
"custom client test"
,
client
:
Client
{
Name
:
"default"
,
RespondWithChallenges
:
true
,
RedirectURIs
:
[]
string
{
AllowAllRedirectURI
,
"https://foo.bar.com/oauth/cb"
},
GrantMethod
:
GrantHandlerAuto
,
ScopeRestrictions
:
[]
string
{
"full"
},
},
expectError
:
nil
,
expectURL
:
"https://foo.bar.com/oauth/err2"
,
},
}
for
_
,
test
:=
range
tests
{
redirectURL
,
err
:=
test
.
client
.
ResolveRedirectURL
(
test
.
expectURL
)
if
err
!=
test
.
expectError
{
t
.
Errorf
(
"expected error: %s, got: %s"
,
test
.
expectError
,
err
)
}
if
test
.
expectError
==
nil
&&
test
.
expectURL
!=
redirectURL
{
t
.
Errorf
(
"expected redirect url: %s, got: %s"
,
test
.
expectURL
,
redirectURL
)
}
}
}
pkg/apiserver/config/config_test.go
浏览文件 @
34dfc204
...
...
@@ -5,8 +5,6 @@ import (
"github.com/google/go-cmp/cmp"
"gopkg.in/yaml.v2"
"io/ioutil"
"kubesphere.io/kubesphere/pkg/apiserver/authentication/identityprovider"
"kubesphere.io/kubesphere/pkg/apiserver/authentication/identityprovider/github"
"kubesphere.io/kubesphere/pkg/apiserver/authentication/oauth"
authoptions
"kubesphere.io/kubesphere/pkg/apiserver/authentication/options"
"kubesphere.io/kubesphere/pkg/simple/client/alerting"
...
...
@@ -29,19 +27,6 @@ import (
func
newTestConfig
()
(
*
Config
,
error
)
{
githubOAuthProvider
,
err
:=
identityprovider
.
ResolveOAuthOptions
(
"github"
,
&
github
.
Github
{
ClientID
:
"de6ff7bed0304e487b6e"
,
ClientSecret
:
"xxxxxx-xxxxx-xxxxx"
,
Endpoint
:
github
.
Endpoint
{
AuthURL
:
"https://github.com/login/oauth/authorize"
,
TokenURL
:
"https://github.com/login/oauth/token"
,
},
RedirectURL
:
"https://ks-console.kubesphere-system.svc/oauth/callbak/github"
,
Scopes
:
[]
string
{
"user"
},
})
if
err
!=
nil
{
return
nil
,
err
}
var
conf
=
&
Config
{
MySQLOptions
:
&
mysql
.
Options
{
Host
:
"10.68.96.5:3306"
,
...
...
@@ -125,13 +110,7 @@ func newTestConfig() (*Config, error) {
JwtSecret
:
"xxxxxx"
,
MultipleLogin
:
false
,
OAuthOptions
:
&
oauth
.
Options
{
IdentityProviders
:
[]
oauth
.
IdentityProviderOptions
{{
Name
:
"github"
,
MappingMethod
:
"auto"
,
LoginRedirect
:
true
,
Type
:
"github"
,
Provider
:
githubOAuthProvider
,
}},
IdentityProviders
:
[]
oauth
.
IdentityProviderOptions
{},
Clients
:
[]
oauth
.
Client
{{
Name
:
"kubesphere-console-client"
,
Secret
:
"xxxxxx-xxxxxx-xxxxxx"
,
...
...
@@ -185,7 +164,6 @@ func TestGet(t *testing.T) {
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
diff
:=
cmp
.
Diff
(
conf
,
conf2
);
diff
!=
""
{
t
.
Fatal
(
diff
)
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录