| // |
| 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 |
| 15 | package auth |
| 16 |
| 17 | import ( |
| 18 | "fmt" |
| 19 | "slices" |
| 20 | "time" |
| 21 |
| 22 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 23 | "github.com/fatedier/frp/pkg/msg" |
| 24 | "github.com/fatedier/frp/pkg/util/util" |
| 25 | ) |
| 26 |
| 27 | type TokenAuthSetterVerifier struct { |
| 28 | additionalAuthScopes []v1.AuthScope |
| 29 | token string |
| 30 | } |
| 31 |
| 32 | func NewTokenAuth(additionalAuthScopes []v1.AuthScope, token string) *TokenAuthSetterVerifier { |
| 33 | return &TokenAuthSetterVerifier{ |
| 34 | additionalAuthScopes: additionalAuthScopes, |
| 35 | token: token, |
| 36 | } |
| 37 | } |
| 38 |
| 39 | func (auth *TokenAuthSetterVerifier) SetLogin(loginMsg *msg.Login) error { |
| 40 | loginMsg.PrivilegeKey = util.GetAuthKey(auth.token, loginMsg.Timestamp) |
| 41 | return nil |
| 42 | } |
| 43 |
| 44 | func (auth *TokenAuthSetterVerifier) SetPing(pingMsg *msg.Ping) error { |
| 45 | if !slices.Contains(auth.additionalAuthScopes, v1.AuthScopeHeartBeats) { |
| 46 | return nil |
| 47 | } |
| 48 |
| 49 | pingMsg.Timestamp = time.Now().Unix() |
| 50 | pingMsg.PrivilegeKey = util.GetAuthKey(auth.token, pingMsg.Timestamp) |
| 51 | return nil |
| 52 | } |
| 53 |
| 54 | func (auth *TokenAuthSetterVerifier) SetNewWorkConn(newWorkConnMsg *msg.NewWorkConn) error { |
| 55 | if !slices.Contains(auth.additionalAuthScopes, v1.AuthScopeNewWorkConns) { |
| 56 | return nil |
| 57 | } |
| 58 |
| 59 | newWorkConnMsg.Timestamp = time.Now().Unix() |
| 60 | newWorkConnMsg.PrivilegeKey = util.GetAuthKey(auth.token, newWorkConnMsg.Timestamp) |
| 61 | return nil |
| 62 | } |
| 63 |
| 64 | func (auth *TokenAuthSetterVerifier) VerifyLogin(m *msg.Login) error { |
| 65 | if !util.ConstantTimeEqString(util.GetAuthKey(auth.token, m.Timestamp), m.PrivilegeKey) { |
| 66 | return fmt.Errorf("token in login doesn't match token from configuration") |
| 67 | } |
| 68 | return nil |
| 69 | } |
| 70 |
| 71 | func (auth *TokenAuthSetterVerifier) VerifyPing(m *msg.Ping) error { |
| 72 | if !slices.Contains(auth.additionalAuthScopes, v1.AuthScopeHeartBeats) { |
| 73 | return nil |
| 74 | } |
| 75 |
| 76 | if !util.ConstantTimeEqString(util.GetAuthKey(auth.token, m.Timestamp), m.PrivilegeKey) { |
| 77 | return fmt.Errorf("token in heartbeat doesn't match token from configuration") |
| 78 | } |
| 79 | return nil |
| 80 | } |
| 81 |
| 82 | func (auth *TokenAuthSetterVerifier) VerifyNewWorkConn(m *msg.NewWorkConn) error { |
| 83 | if !slices.Contains(auth.additionalAuthScopes, v1.AuthScopeNewWorkConns) { |
| 84 | return nil |
| 85 | } |
| 86 |
| 87 | if !util.ConstantTimeEqString(util.GetAuthKey(auth.token, m.Timestamp), m.PrivilegeKey) { |
| 88 | return fmt.Errorf("token in NewWorkConn doesn't match token from configuration") |
| 89 | } |
| 90 | return nil |
| 91 | } |