token.go 3.1 KB

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