auth.go 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2020 guylewin, guy@lewin.co.il
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package auth
  15. import (
  16. "fmt"
  17. "github.com/fatedier/frp/pkg/consts"
  18. "github.com/fatedier/frp/pkg/msg"
  19. )
  20. type BaseConfig struct {
  21. // AuthenticationMethod specifies what authentication method to use to
  22. // authenticate frpc with frps. If "token" is specified - token will be
  23. // read into login message. If "oidc" is specified - OIDC (Open ID Connect)
  24. // token will be issued using OIDC settings. By default, this value is "token".
  25. AuthenticationMethod string `ini:"authentication_method" json:"authentication_method"`
  26. // AuthenticateHeartBeats specifies whether to include authentication token in
  27. // heartbeats sent to frps. By default, this value is false.
  28. AuthenticateHeartBeats bool `ini:"authenticate_heartbeats" json:"authenticate_heartbeats"`
  29. // AuthenticateNewWorkConns specifies whether to include authentication token in
  30. // new work connections sent to frps. By default, this value is false.
  31. AuthenticateNewWorkConns bool `ini:"authenticate_new_work_conns" json:"authenticate_new_work_conns"`
  32. }
  33. func getDefaultBaseConf() BaseConfig {
  34. return BaseConfig{
  35. AuthenticationMethod: "token",
  36. AuthenticateHeartBeats: false,
  37. AuthenticateNewWorkConns: false,
  38. }
  39. }
  40. type ClientConfig struct {
  41. BaseConfig `ini:",extends"`
  42. OidcClientConfig `ini:",extends"`
  43. TokenConfig `ini:",extends"`
  44. }
  45. func GetDefaultClientConf() ClientConfig {
  46. return ClientConfig{
  47. BaseConfig: getDefaultBaseConf(),
  48. OidcClientConfig: getDefaultOidcClientConf(),
  49. TokenConfig: getDefaultTokenConf(),
  50. }
  51. }
  52. type ServerConfig struct {
  53. BaseConfig `ini:",extends"`
  54. OidcServerConfig `ini:",extends"`
  55. TokenConfig `ini:",extends"`
  56. }
  57. func GetDefaultServerConf() ServerConfig {
  58. return ServerConfig{
  59. BaseConfig: getDefaultBaseConf(),
  60. OidcServerConfig: getDefaultOidcServerConf(),
  61. TokenConfig: getDefaultTokenConf(),
  62. }
  63. }
  64. type Setter interface {
  65. SetLogin(*msg.Login) error
  66. SetPing(*msg.Ping) error
  67. SetNewWorkConn(*msg.NewWorkConn) error
  68. }
  69. func NewAuthSetter(cfg ClientConfig) (authProvider Setter) {
  70. switch cfg.AuthenticationMethod {
  71. case consts.TokenAuthMethod:
  72. authProvider = NewTokenAuth(cfg.BaseConfig, cfg.TokenConfig)
  73. case consts.OidcAuthMethod:
  74. authProvider = NewOidcAuthSetter(cfg.BaseConfig, cfg.OidcClientConfig)
  75. default:
  76. panic(fmt.Sprintf("wrong authentication method: '%s'", cfg.AuthenticationMethod))
  77. }
  78. return authProvider
  79. }
  80. type Verifier interface {
  81. VerifyLogin(*msg.Login) error
  82. VerifyPing(*msg.Ping) error
  83. VerifyNewWorkConn(*msg.NewWorkConn) error
  84. }
  85. func NewAuthVerifier(cfg ServerConfig) (authVerifier Verifier) {
  86. switch cfg.AuthenticationMethod {
  87. case consts.TokenAuthMethod:
  88. authVerifier = NewTokenAuth(cfg.BaseConfig, cfg.TokenConfig)
  89. case consts.OidcAuthMethod:
  90. authVerifier = NewOidcAuthVerifier(cfg.BaseConfig, cfg.OidcServerConfig)
  91. }
  92. return authVerifier
  93. }