2020-03-01 10:57:01 +08:00
|
|
|
// Copyright 2020 guylewin, guy@lewin.co.il
|
|
|
|
//
|
|
|
|
// 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 auth
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
|
2022-12-12 15:10:38 +08:00
|
|
|
"github.com/coreos/go-oidc/v3/oidc"
|
2023-09-06 10:18:02 +08:00
|
|
|
"github.com/samber/lo"
|
2020-03-01 10:57:01 +08:00
|
|
|
"golang.org/x/oauth2/clientcredentials"
|
2022-08-29 01:02:53 +08:00
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
v1 "github.com/fatedier/frp/pkg/config/v1"
|
2022-08-29 01:02:53 +08:00
|
|
|
"github.com/fatedier/frp/pkg/msg"
|
2020-03-01 10:57:01 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type OidcAuthProvider struct {
|
2023-09-06 10:18:02 +08:00
|
|
|
additionalAuthScopes []v1.AuthScope
|
2020-03-01 10:57:01 +08:00
|
|
|
|
|
|
|
tokenGenerator *clientcredentials.Config
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
func NewOidcAuthSetter(additionalAuthScopes []v1.AuthScope, cfg v1.AuthOIDCClientConfig) *OidcAuthProvider {
|
2022-03-07 14:23:49 +08:00
|
|
|
eps := make(map[string][]string)
|
2023-09-06 10:18:02 +08:00
|
|
|
for k, v := range cfg.AdditionalEndpointParams {
|
2022-03-07 14:23:49 +08:00
|
|
|
eps[k] = []string{v}
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
if cfg.Audience != "" {
|
|
|
|
eps["audience"] = []string{cfg.Audience}
|
2022-12-09 11:46:34 +08:00
|
|
|
}
|
|
|
|
|
2020-03-01 10:57:01 +08:00
|
|
|
tokenGenerator := &clientcredentials.Config{
|
2023-09-06 10:18:02 +08:00
|
|
|
ClientID: cfg.ClientID,
|
|
|
|
ClientSecret: cfg.ClientSecret,
|
|
|
|
Scopes: []string{cfg.Scope},
|
|
|
|
TokenURL: cfg.TokenEndpointURL,
|
2022-03-07 14:23:49 +08:00
|
|
|
EndpointParams: eps,
|
2020-03-01 10:57:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return &OidcAuthProvider{
|
2023-09-06 10:18:02 +08:00
|
|
|
additionalAuthScopes: additionalAuthScopes,
|
|
|
|
tokenGenerator: tokenGenerator,
|
2020-03-01 10:57:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (auth *OidcAuthProvider) generateAccessToken() (accessToken string, err error) {
|
|
|
|
tokenObj, err := auth.tokenGenerator.Token(context.Background())
|
|
|
|
if err != nil {
|
|
|
|
return "", fmt.Errorf("couldn't generate OIDC token for login: %v", err)
|
|
|
|
}
|
|
|
|
return tokenObj.AccessToken, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (auth *OidcAuthProvider) SetLogin(loginMsg *msg.Login) (err error) {
|
|
|
|
loginMsg.PrivilegeKey, err = auth.generateAccessToken()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (auth *OidcAuthProvider) SetPing(pingMsg *msg.Ping) (err error) {
|
2023-09-06 10:18:02 +08:00
|
|
|
if !lo.Contains(auth.additionalAuthScopes, v1.AuthScopeHeartBeats) {
|
2020-03-01 10:57:01 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
pingMsg.PrivilegeKey, err = auth.generateAccessToken()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (auth *OidcAuthProvider) SetNewWorkConn(newWorkConnMsg *msg.NewWorkConn) (err error) {
|
2023-09-06 10:18:02 +08:00
|
|
|
if !lo.Contains(auth.additionalAuthScopes, v1.AuthScopeNewWorkConns) {
|
2020-03-01 10:57:01 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
newWorkConnMsg.PrivilegeKey, err = auth.generateAccessToken()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
type OidcAuthConsumer struct {
|
2023-09-06 10:18:02 +08:00
|
|
|
additionalAuthScopes []v1.AuthScope
|
2020-03-01 10:57:01 +08:00
|
|
|
|
|
|
|
verifier *oidc.IDTokenVerifier
|
|
|
|
subjectFromLogin string
|
|
|
|
}
|
|
|
|
|
2023-09-06 10:18:02 +08:00
|
|
|
func NewOidcAuthVerifier(additionalAuthScopes []v1.AuthScope, cfg v1.AuthOIDCServerConfig) *OidcAuthConsumer {
|
|
|
|
provider, err := oidc.NewProvider(context.Background(), cfg.Issuer)
|
2020-03-01 10:57:01 +08:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
verifierConf := oidc.Config{
|
2023-09-06 10:18:02 +08:00
|
|
|
ClientID: cfg.Audience,
|
|
|
|
SkipClientIDCheck: cfg.Audience == "",
|
|
|
|
SkipExpiryCheck: cfg.SkipExpiryCheck,
|
|
|
|
SkipIssuerCheck: cfg.SkipIssuerCheck,
|
2020-03-01 10:57:01 +08:00
|
|
|
}
|
|
|
|
return &OidcAuthConsumer{
|
2023-09-06 10:18:02 +08:00
|
|
|
additionalAuthScopes: additionalAuthScopes,
|
|
|
|
verifier: provider.Verifier(&verifierConf),
|
2020-03-01 10:57:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (auth *OidcAuthConsumer) VerifyLogin(loginMsg *msg.Login) (err error) {
|
|
|
|
token, err := auth.verifier.Verify(context.Background(), loginMsg.PrivilegeKey)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid OIDC token in login: %v", err)
|
|
|
|
}
|
|
|
|
auth.subjectFromLogin = token.Subject
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (auth *OidcAuthConsumer) verifyPostLoginToken(privilegeKey string) (err error) {
|
|
|
|
token, err := auth.verifier.Verify(context.Background(), privilegeKey)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("invalid OIDC token in ping: %v", err)
|
|
|
|
}
|
|
|
|
if token.Subject != auth.subjectFromLogin {
|
|
|
|
return fmt.Errorf("received different OIDC subject in login and ping. "+
|
|
|
|
"original subject: %s, "+
|
|
|
|
"new subject: %s",
|
|
|
|
auth.subjectFromLogin, token.Subject)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (auth *OidcAuthConsumer) VerifyPing(pingMsg *msg.Ping) (err error) {
|
2023-09-06 10:18:02 +08:00
|
|
|
if !lo.Contains(auth.additionalAuthScopes, v1.AuthScopeHeartBeats) {
|
2020-03-01 10:57:01 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return auth.verifyPostLoginToken(pingMsg.PrivilegeKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (auth *OidcAuthConsumer) VerifyNewWorkConn(newWorkConnMsg *msg.NewWorkConn) (err error) {
|
2023-09-06 10:18:02 +08:00
|
|
|
if !lo.Contains(auth.additionalAuthScopes, v1.AuthScopeNewWorkConns) {
|
2020-03-01 10:57:01 +08:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return auth.verifyPostLoginToken(newWorkConnMsg.PrivilegeKey)
|
|
|
|
}
|