1
0

auth.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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/models/consts"
  18. "github.com/fatedier/frp/models/msg"
  19. "github.com/vaughan0/go-ini"
  20. )
  21. type baseConfig struct {
  22. // AuthenticationMethod specifies what authentication method to use to
  23. // authenticate frpc with frps. If "token" is specified - token will be
  24. // read into login message. If "oidc" is specified - OIDC (Open ID Connect)
  25. // token will be issued using OIDC settings. By default, this value is "token".
  26. AuthenticationMethod string `json:"authentication_method"`
  27. // AuthenticateHeartBeats specifies whether to include authentication token in
  28. // heartbeats sent to frps. By default, this value is false.
  29. AuthenticateHeartBeats bool `json:"authenticate_heartbeats"`
  30. // AuthenticateNewWorkConns specifies whether to include authentication token in
  31. // new work connections sent to frps. By default, this value is false.
  32. AuthenticateNewWorkConns bool `json:"authenticate_new_work_conns"`
  33. }
  34. func getDefaultBaseConf() baseConfig {
  35. return baseConfig{
  36. AuthenticationMethod: "token",
  37. AuthenticateHeartBeats: false,
  38. AuthenticateNewWorkConns: false,
  39. }
  40. }
  41. func unmarshalBaseConfFromIni(conf ini.File) baseConfig {
  42. var (
  43. tmpStr string
  44. ok bool
  45. )
  46. cfg := getDefaultBaseConf()
  47. if tmpStr, ok = conf.Get("common", "authentication_method"); ok {
  48. cfg.AuthenticationMethod = tmpStr
  49. }
  50. if tmpStr, ok = conf.Get("common", "authenticate_heartbeats"); ok && tmpStr == "true" {
  51. cfg.AuthenticateHeartBeats = true
  52. } else {
  53. cfg.AuthenticateHeartBeats = false
  54. }
  55. if tmpStr, ok = conf.Get("common", "authenticate_new_work_conns"); ok && tmpStr == "true" {
  56. cfg.AuthenticateNewWorkConns = true
  57. } else {
  58. cfg.AuthenticateNewWorkConns = false
  59. }
  60. return cfg
  61. }
  62. type ClientConfig struct {
  63. baseConfig
  64. oidcClientConfig
  65. tokenConfig
  66. }
  67. func GetDefaultClientConf() ClientConfig {
  68. return ClientConfig{
  69. baseConfig: getDefaultBaseConf(),
  70. oidcClientConfig: getDefaultOidcClientConf(),
  71. tokenConfig: getDefaultTokenConf(),
  72. }
  73. }
  74. func UnmarshalClientConfFromIni(conf ini.File) (cfg ClientConfig) {
  75. cfg.baseConfig = unmarshalBaseConfFromIni(conf)
  76. cfg.oidcClientConfig = unmarshalOidcClientConfFromIni(conf)
  77. cfg.tokenConfig = unmarshalTokenConfFromIni(conf)
  78. return cfg
  79. }
  80. type ServerConfig struct {
  81. baseConfig
  82. oidcServerConfig
  83. tokenConfig
  84. }
  85. func GetDefaultServerConf() ServerConfig {
  86. return ServerConfig{
  87. baseConfig: getDefaultBaseConf(),
  88. oidcServerConfig: getDefaultOidcServerConf(),
  89. tokenConfig: getDefaultTokenConf(),
  90. }
  91. }
  92. func UnmarshalServerConfFromIni(conf ini.File) (cfg ServerConfig) {
  93. cfg.baseConfig = unmarshalBaseConfFromIni(conf)
  94. cfg.oidcServerConfig = unmarshalOidcServerConfFromIni(conf)
  95. cfg.tokenConfig = unmarshalTokenConfFromIni(conf)
  96. return cfg
  97. }
  98. type Setter interface {
  99. SetLogin(*msg.Login) error
  100. SetPing(*msg.Ping) error
  101. SetNewWorkConn(*msg.NewWorkConn) error
  102. }
  103. func NewAuthSetter(cfg ClientConfig) (authProvider Setter) {
  104. switch cfg.AuthenticationMethod {
  105. case consts.TokenAuthMethod:
  106. authProvider = NewTokenAuth(cfg.baseConfig, cfg.tokenConfig)
  107. case consts.OidcAuthMethod:
  108. authProvider = NewOidcAuthSetter(cfg.baseConfig, cfg.oidcClientConfig)
  109. default:
  110. panic(fmt.Sprintf("wrong authentication method: '%s'", cfg.AuthenticationMethod))
  111. }
  112. return authProvider
  113. }
  114. type Verifier interface {
  115. VerifyLogin(*msg.Login) error
  116. VerifyPing(*msg.Ping) error
  117. VerifyNewWorkConn(*msg.NewWorkConn) error
  118. }
  119. func NewAuthVerifier(cfg ServerConfig) (authVerifier Verifier) {
  120. switch cfg.AuthenticationMethod {
  121. case consts.TokenAuthMethod:
  122. authVerifier = NewTokenAuth(cfg.baseConfig, cfg.tokenConfig)
  123. case consts.OidcAuthMethod:
  124. authVerifier = NewOidcAuthVerifier(cfg.baseConfig, cfg.oidcServerConfig)
  125. }
  126. return authVerifier
  127. }