token.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. "time"
  18. "github.com/fatedier/frp/pkg/msg"
  19. "github.com/fatedier/frp/pkg/util/util"
  20. )
  21. type TokenConfig struct {
  22. // Token specifies the authorization token used to create keys to be sent
  23. // to the server. The server must have a matching token for authorization
  24. // to succeed. By default, this value is "".
  25. Token string `ini:"token" json:"token"`
  26. }
  27. func getDefaultTokenConf() TokenConfig {
  28. return TokenConfig{
  29. Token: "",
  30. }
  31. }
  32. type TokenAuthSetterVerifier struct {
  33. BaseConfig
  34. token string
  35. }
  36. func NewTokenAuth(baseCfg BaseConfig, cfg TokenConfig) *TokenAuthSetterVerifier {
  37. return &TokenAuthSetterVerifier{
  38. BaseConfig: baseCfg,
  39. token: cfg.Token,
  40. }
  41. }
  42. func (auth *TokenAuthSetterVerifier) SetLogin(loginMsg *msg.Login) (err error) {
  43. loginMsg.PrivilegeKey = util.GetAuthKey(auth.token, loginMsg.Timestamp)
  44. return nil
  45. }
  46. func (auth *TokenAuthSetterVerifier) SetPing(pingMsg *msg.Ping) error {
  47. if !auth.AuthenticateHeartBeats {
  48. return nil
  49. }
  50. pingMsg.Timestamp = time.Now().Unix()
  51. pingMsg.PrivilegeKey = util.GetAuthKey(auth.token, pingMsg.Timestamp)
  52. return nil
  53. }
  54. func (auth *TokenAuthSetterVerifier) SetNewWorkConn(newWorkConnMsg *msg.NewWorkConn) error {
  55. if !auth.AuthenticateNewWorkConns {
  56. return nil
  57. }
  58. newWorkConnMsg.Timestamp = time.Now().Unix()
  59. newWorkConnMsg.PrivilegeKey = util.GetAuthKey(auth.token, newWorkConnMsg.Timestamp)
  60. return nil
  61. }
  62. func (auth *TokenAuthSetterVerifier) VerifyLogin(loginMsg *msg.Login) error {
  63. if util.GetAuthKey(auth.token, loginMsg.Timestamp) != loginMsg.PrivilegeKey {
  64. return fmt.Errorf("token in login doesn't match token from configuration")
  65. }
  66. return nil
  67. }
  68. func (auth *TokenAuthSetterVerifier) VerifyPing(pingMsg *msg.Ping) error {
  69. if !auth.AuthenticateHeartBeats {
  70. return nil
  71. }
  72. if util.GetAuthKey(auth.token, pingMsg.Timestamp) != pingMsg.PrivilegeKey {
  73. return fmt.Errorf("token in heartbeat doesn't match token from configuration")
  74. }
  75. return nil
  76. }
  77. func (auth *TokenAuthSetterVerifier) VerifyNewWorkConn(newWorkConnMsg *msg.NewWorkConn) error {
  78. if !auth.AuthenticateNewWorkConns {
  79. return nil
  80. }
  81. if util.GetAuthKey(auth.token, newWorkConnMsg.Timestamp) != newWorkConnMsg.PrivilegeKey {
  82. return fmt.Errorf("token in NewWorkConn doesn't match token from configuration")
  83. }
  84. return nil
  85. }