auth.go 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// 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"

F
fatedier 已提交
20 21
	"github.com/fatedier/frp/pkg/consts"
	"github.com/fatedier/frp/pkg/msg"
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

	"github.com/vaughan0/go-ini"
)

type baseConfig struct {
	// 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".
	AuthenticationMethod string `json:"authentication_method"`
	// AuthenticateHeartBeats specifies whether to include authentication token in
	// heartbeats sent to frps. By default, this value is false.
	AuthenticateHeartBeats bool `json:"authenticate_heartbeats"`
	// AuthenticateNewWorkConns specifies whether to include authentication token in
	// new work connections sent to frps. By default, this value is false.
	AuthenticateNewWorkConns bool `json:"authenticate_new_work_conns"`
}

func getDefaultBaseConf() baseConfig {
	return baseConfig{
		AuthenticationMethod:     "token",
		AuthenticateHeartBeats:   false,
		AuthenticateNewWorkConns: false,
	}
}

func unmarshalBaseConfFromIni(conf ini.File) baseConfig {
	var (
		tmpStr string
		ok     bool
	)

	cfg := getDefaultBaseConf()

	if tmpStr, ok = conf.Get("common", "authentication_method"); ok {
		cfg.AuthenticationMethod = tmpStr
	}

	if tmpStr, ok = conf.Get("common", "authenticate_heartbeats"); ok && tmpStr == "true" {
		cfg.AuthenticateHeartBeats = true
	} else {
		cfg.AuthenticateHeartBeats = false
	}

	if tmpStr, ok = conf.Get("common", "authenticate_new_work_conns"); ok && tmpStr == "true" {
		cfg.AuthenticateNewWorkConns = true
	} else {
		cfg.AuthenticateNewWorkConns = false
	}

	return cfg
}

F
fatedier 已提交
75
type ClientConfig struct {
76 77 78 79 80
	baseConfig
	oidcClientConfig
	tokenConfig
}

F
fatedier 已提交
81 82
func GetDefaultClientConf() ClientConfig {
	return ClientConfig{
83 84 85 86 87 88
		baseConfig:       getDefaultBaseConf(),
		oidcClientConfig: getDefaultOidcClientConf(),
		tokenConfig:      getDefaultTokenConf(),
	}
}

F
fatedier 已提交
89
func UnmarshalClientConfFromIni(conf ini.File) (cfg ClientConfig) {
90 91 92 93 94 95
	cfg.baseConfig = unmarshalBaseConfFromIni(conf)
	cfg.oidcClientConfig = unmarshalOidcClientConfFromIni(conf)
	cfg.tokenConfig = unmarshalTokenConfFromIni(conf)
	return cfg
}

F
fatedier 已提交
96
type ServerConfig struct {
97 98 99 100 101
	baseConfig
	oidcServerConfig
	tokenConfig
}

F
fatedier 已提交
102 103
func GetDefaultServerConf() ServerConfig {
	return ServerConfig{
104 105 106 107 108 109
		baseConfig:       getDefaultBaseConf(),
		oidcServerConfig: getDefaultOidcServerConf(),
		tokenConfig:      getDefaultTokenConf(),
	}
}

F
fatedier 已提交
110
func UnmarshalServerConfFromIni(conf ini.File) (cfg ServerConfig) {
111 112 113 114 115 116 117 118 119 120 121 122
	cfg.baseConfig = unmarshalBaseConfFromIni(conf)
	cfg.oidcServerConfig = unmarshalOidcServerConfFromIni(conf)
	cfg.tokenConfig = unmarshalTokenConfFromIni(conf)
	return cfg
}

type Setter interface {
	SetLogin(*msg.Login) error
	SetPing(*msg.Ping) error
	SetNewWorkConn(*msg.NewWorkConn) error
}

F
fatedier 已提交
123
func NewAuthSetter(cfg ClientConfig) (authProvider Setter) {
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
	switch cfg.AuthenticationMethod {
	case consts.TokenAuthMethod:
		authProvider = NewTokenAuth(cfg.baseConfig, cfg.tokenConfig)
	case consts.OidcAuthMethod:
		authProvider = NewOidcAuthSetter(cfg.baseConfig, cfg.oidcClientConfig)
	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
}

F
fatedier 已提交
142
func NewAuthVerifier(cfg ServerConfig) (authVerifier Verifier) {
143 144 145 146 147 148 149 150 151
	switch cfg.AuthenticationMethod {
	case consts.TokenAuthMethod:
		authVerifier = NewTokenAuth(cfg.baseConfig, cfg.tokenConfig)
	case consts.OidcAuthMethod:
		authVerifier = NewOidcAuthVerifier(cfg.baseConfig, cfg.oidcServerConfig)
	}

	return authVerifier
}