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 (
|
|
|
|
"fmt"
|
|
|
|
|
2020-09-23 13:49:14 +08:00
|
|
|
"github.com/fatedier/frp/pkg/consts"
|
|
|
|
"github.com/fatedier/frp/pkg/msg"
|
2020-03-01 10:57:01 +08:00
|
|
|
)
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
type BaseConfig struct {
|
2020-03-01 10:57:01 +08:00
|
|
|
// AuthenticationMethod specifies what authentication method to use to
|
|
|
|
// authenticate frpc with frps. If "token" is specified - token will be
|
|
|
|
// read into login message. If "oidc" is specified - OIDC (Open ID Connect)
|
|
|
|
// token will be issued using OIDC settings. By default, this value is "token".
|
2021-01-26 11:31:08 +08:00
|
|
|
AuthenticationMethod string `ini:"authentication_method" json:"authentication_method"`
|
2020-03-01 10:57:01 +08:00
|
|
|
// AuthenticateHeartBeats specifies whether to include authentication token in
|
|
|
|
// heartbeats sent to frps. By default, this value is false.
|
2021-01-26 11:31:08 +08:00
|
|
|
AuthenticateHeartBeats bool `ini:"authenticate_heartbeats" json:"authenticate_heartbeats"`
|
2020-03-01 10:57:01 +08:00
|
|
|
// AuthenticateNewWorkConns specifies whether to include authentication token in
|
|
|
|
// new work connections sent to frps. By default, this value is false.
|
2021-01-26 11:31:08 +08:00
|
|
|
AuthenticateNewWorkConns bool `ini:"authenticate_new_work_conns" json:"authenticate_new_work_conns"`
|
2020-03-01 10:57:01 +08:00
|
|
|
}
|
|
|
|
|
2021-01-26 11:31:08 +08:00
|
|
|
func getDefaultBaseConf() BaseConfig {
|
|
|
|
return BaseConfig{
|
2020-03-01 10:57:01 +08:00
|
|
|
AuthenticationMethod: "token",
|
|
|
|
AuthenticateHeartBeats: false,
|
|
|
|
AuthenticateNewWorkConns: false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
type ClientConfig struct {
|
2021-01-26 11:31:08 +08:00
|
|
|
BaseConfig `ini:",extends"`
|
|
|
|
OidcClientConfig `ini:",extends"`
|
|
|
|
TokenConfig `ini:",extends"`
|
2020-03-01 10:57:01 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func GetDefaultClientConf() ClientConfig {
|
|
|
|
return ClientConfig{
|
2021-01-26 11:31:08 +08:00
|
|
|
BaseConfig: getDefaultBaseConf(),
|
|
|
|
OidcClientConfig: getDefaultOidcClientConf(),
|
|
|
|
TokenConfig: getDefaultTokenConf(),
|
2020-03-01 10:57:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
type ServerConfig struct {
|
2021-01-26 11:31:08 +08:00
|
|
|
BaseConfig `ini:",extends"`
|
|
|
|
OidcServerConfig `ini:",extends"`
|
|
|
|
TokenConfig `ini:",extends"`
|
2020-03-01 10:57:01 +08:00
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func GetDefaultServerConf() ServerConfig {
|
|
|
|
return ServerConfig{
|
2021-01-26 11:31:08 +08:00
|
|
|
BaseConfig: getDefaultBaseConf(),
|
|
|
|
OidcServerConfig: getDefaultOidcServerConf(),
|
|
|
|
TokenConfig: getDefaultTokenConf(),
|
2020-03-01 10:57:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Setter interface {
|
|
|
|
SetLogin(*msg.Login) error
|
|
|
|
SetPing(*msg.Ping) error
|
|
|
|
SetNewWorkConn(*msg.NewWorkConn) error
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func NewAuthSetter(cfg ClientConfig) (authProvider Setter) {
|
2020-03-01 10:57:01 +08:00
|
|
|
switch cfg.AuthenticationMethod {
|
|
|
|
case consts.TokenAuthMethod:
|
2021-01-26 11:31:08 +08:00
|
|
|
authProvider = NewTokenAuth(cfg.BaseConfig, cfg.TokenConfig)
|
2020-03-01 10:57:01 +08:00
|
|
|
case consts.OidcAuthMethod:
|
2021-01-26 11:31:08 +08:00
|
|
|
authProvider = NewOidcAuthSetter(cfg.BaseConfig, cfg.OidcClientConfig)
|
2020-03-01 10:57:01 +08:00
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("wrong authentication method: '%s'", cfg.AuthenticationMethod))
|
|
|
|
}
|
|
|
|
|
|
|
|
return authProvider
|
|
|
|
}
|
|
|
|
|
|
|
|
type Verifier interface {
|
|
|
|
VerifyLogin(*msg.Login) error
|
|
|
|
VerifyPing(*msg.Ping) error
|
|
|
|
VerifyNewWorkConn(*msg.NewWorkConn) error
|
|
|
|
}
|
|
|
|
|
2020-05-24 17:48:37 +08:00
|
|
|
func NewAuthVerifier(cfg ServerConfig) (authVerifier Verifier) {
|
2020-03-01 10:57:01 +08:00
|
|
|
switch cfg.AuthenticationMethod {
|
|
|
|
case consts.TokenAuthMethod:
|
2021-01-26 11:31:08 +08:00
|
|
|
authVerifier = NewTokenAuth(cfg.BaseConfig, cfg.TokenConfig)
|
2020-03-01 10:57:01 +08:00
|
|
|
case consts.OidcAuthMethod:
|
2021-01-26 11:31:08 +08:00
|
|
|
authVerifier = NewOidcAuthVerifier(cfg.BaseConfig, cfg.OidcServerConfig)
|
2020-03-01 10:57:01 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return authVerifier
|
|
|
|
}
|