| // |
| 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 | "context" |
| 19 | "fmt" |
| 20 |
| 21 | v1 "github.com/fatedier/frp/pkg/config/v1" |
| 22 | "github.com/fatedier/frp/pkg/msg" |
| 23 | ) |
| 24 |
| 25 | type Setter interface { |
| 26 | SetLogin(*msg.Login) error |
| 27 | SetPing(*msg.Ping) error |
| 28 | SetNewWorkConn(*msg.NewWorkConn) error |
| 29 | } |
| 30 |
| 31 | type ClientAuth struct { |
| 32 | Setter Setter |
| 33 | key []byte |
| 34 | } |
| 35 |
| 36 | func (a *ClientAuth) EncryptionKey() []byte { |
| 37 | return a.key |
| 38 | } |
| 39 |
| 40 | // BuildClientAuth resolves any dynamic auth values and returns a prepared auth runtime. |
| 41 | // Caller must run validation before calling this function. |
| 42 | func BuildClientAuth(cfg *v1.AuthClientConfig) (*ClientAuth, error) { |
| 43 | if cfg == nil { |
| 44 | return nil, fmt.Errorf("auth config is nil") |
| 45 | } |
| 46 | resolved := *cfg |
| 47 | if resolved.Method == v1.AuthMethodToken && resolved.TokenSource != nil { |
| 48 | token, err := resolved.TokenSource.Resolve(context.Background()) |
| 49 | if err != nil { |
| 50 | return nil, fmt.Errorf("failed to resolve auth.tokenSource: %w", err) |
| 51 | } |
| 52 | resolved.Token = token |
| 53 | } |
| 54 | setter, err := NewAuthSetter(resolved) |
| 55 | if err != nil { |
| 56 | return nil, err |
| 57 | } |
| 58 | return &ClientAuth{ |
| 59 | Setter: setter, |
| 60 | key: []byte(resolved.Token), |
| 61 | }, nil |
| 62 | } |
| 63 |
| 64 | func NewAuthSetter(cfg v1.AuthClientConfig) (authProvider Setter, err error) { |
| 65 | switch cfg.Method { |
| 66 | case v1.AuthMethodToken: |
| 67 | authProvider = NewTokenAuth(cfg.AdditionalScopes, cfg.Token) |
| 68 | case v1.AuthMethodOIDC: |
| 69 | if cfg.OIDC.TokenSource != nil { |
| 70 | authProvider = NewOidcTokenSourceAuthSetter(cfg.AdditionalScopes, cfg.OIDC.TokenSource) |
| 71 | } else { |
| 72 | authProvider, err = NewOidcAuthSetter(cfg.AdditionalScopes, cfg.OIDC) |
| 73 | if err != nil { |
| 74 | return nil, err |
| 75 | } |
| 76 | } |
| 77 | default: |
| 78 | return nil, fmt.Errorf("unsupported auth method: %s", cfg.Method) |
| 79 | } |
| 80 | return authProvider, nil |
| 81 | } |
| 82 |
| 83 | type Verifier interface { |
| 84 | VerifyLogin(*msg.Login) error |
| 85 | VerifyPing(*msg.Ping) error |
| 86 | VerifyNewWorkConn(*msg.NewWorkConn) error |
| 87 | } |
| 88 |
| 89 | type ServerAuth struct { |
| 90 | Verifier Verifier |
| 91 | key []byte |
| 92 | } |
| 93 |
| 94 | func (a *ServerAuth) EncryptionKey() []byte { |
| 95 | return a.key |
| 96 | } |
| 97 |
| 98 | // BuildServerAuth resolves any dynamic auth values and returns a prepared auth runtime. |
| 99 | // Caller must run validation before calling this function. |
| 100 | func BuildServerAuth(cfg *v1.AuthServerConfig) (*ServerAuth, error) { |
| 101 | if cfg == nil { |
| 102 | return nil, fmt.Errorf("auth config is nil") |
| 103 | } |
| 104 | resolved := *cfg |
| 105 | if resolved.Method == v1.AuthMethodToken && resolved.TokenSource != nil { |
| 106 | token, err := resolved.TokenSource.Resolve(context.Background()) |
| 107 | if err != nil { |
| 108 | return nil, fmt.Errorf("failed to resolve auth.tokenSource: %w", err) |
| 109 | } |
| 110 | resolved.Token = token |
| 111 | } |
| 112 | return &ServerAuth{ |
| 113 | Verifier: NewAuthVerifier(resolved), |
| 114 | key: []byte(resolved.Token), |
| 115 | }, nil |
| 116 | } |
| 117 |
| 118 | func NewAuthVerifier(cfg v1.AuthServerConfig) (authVerifier Verifier) { |
| 119 | switch cfg.Method { |
| 120 | case v1.AuthMethodToken: |
| 121 | authVerifier = NewTokenAuth(cfg.AdditionalScopes, cfg.Token) |
| 122 | case v1.AuthMethodOIDC: |
| 123 | tokenVerifier := NewTokenVerifier(cfg.OIDC) |
| 124 | authVerifier = NewOidcAuthVerifier(cfg.AdditionalScopes, tokenVerifier) |
| 125 | } |
| 126 | return authVerifier |
| 127 | } |