-
-
Notifications
You must be signed in to change notification settings - Fork 410
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
faet(server): add authentication middleware
- Loading branch information
Showing
17 changed files
with
616 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package auth | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/pelletier/go-toml/v2" | ||
) | ||
|
||
// Read reads the toml file specified by the filepath given. | ||
func Read(filepath string) (settings Settings, err error) { | ||
file, err := os.Open(filepath) | ||
if err != nil { | ||
return settings, fmt.Errorf("opening file: %w", err) | ||
} | ||
decoder := toml.NewDecoder(file) | ||
decoder.DisallowUnknownFields() | ||
err = decoder.Decode(&settings) | ||
if err != nil { | ||
strictErr := new(toml.StrictMissingError) | ||
ok := errors.As(err, &strictErr) | ||
if ok { | ||
return settings, fmt.Errorf("toml decoding file: %w:\n%s", | ||
strictErr, strictErr.String()) | ||
} | ||
return settings, fmt.Errorf("toml decoding file: %w", err) | ||
} | ||
return settings, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package auth | ||
|
||
import ( | ||
"io/fs" | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
// Read reads the toml file specified by the filepath given. | ||
func Test_Read(t *testing.T) { | ||
t.Parallel() | ||
|
||
testCases := map[string]struct { | ||
fileContent string | ||
settings Settings | ||
errMessage string | ||
}{ | ||
"empty_file": {}, | ||
"unknown field": { | ||
fileContent: `unknown = "what is this"`, | ||
errMessage: `toml decoding file: strict mode: fields in the document are missing in the target struct: | ||
1| unknown = "what is this" | ||
| ~~~~~~~ missing field`, | ||
}, | ||
"filled_settings": { | ||
fileContent: `[[auths]] | ||
name = "abc" | ||
method = "none" | ||
[[auths]] | ||
name = "xyz" | ||
method = "oauth2" | ||
[[roles]] | ||
name = "public" | ||
auths = ["abc"] | ||
[[roles.routes]] | ||
Method = 'GET' | ||
Path = '/v1/vpn/status'`, | ||
settings: Settings{ | ||
Auths: []Auth{{ | ||
Name: "abc", | ||
Method: MethodNone, | ||
}, { | ||
Name: "xyz", | ||
Method: "oauth2", | ||
}}, | ||
Roles: []Role{{ | ||
Name: "public", | ||
Auths: []string{"abc"}, | ||
Routes: []Route{{Method: "GET", Path: "/v1/vpn/status"}}, | ||
}}, | ||
}, | ||
}, | ||
} | ||
|
||
for name, testCase := range testCases { | ||
testCase := testCase | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
tempDir := t.TempDir() | ||
filepath := tempDir + "/config.toml" | ||
const permissions fs.FileMode = 0600 | ||
err := os.WriteFile(filepath, []byte(testCase.fileContent), permissions) | ||
require.NoError(t, err) | ||
|
||
settings, err := Read(filepath) | ||
|
||
assert.Equal(t, testCase.settings, settings) | ||
if testCase.errMessage != "" { | ||
assert.EqualError(t, err, testCase.errMessage) | ||
} else { | ||
assert.NoError(t, err) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package auth | ||
|
||
func andStrings(strings []string) (result string) { | ||
return joinStrings(strings, "and") | ||
} | ||
|
||
func orStrings(strings []string) (result string) { | ||
return joinStrings(strings, "or") | ||
} | ||
|
||
func joinStrings(strings []string, lastJoin string) (result string) { | ||
if len(strings) == 0 { | ||
return "" | ||
} | ||
|
||
result = strings[0] | ||
for i := 1; i < len(strings); i++ { | ||
if i < len(strings)-1 { | ||
result += ", " + strings[i] | ||
} else { | ||
result += " " + lastJoin + " " + strings[i] | ||
} | ||
} | ||
|
||
return result | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package auth | ||
|
||
type DebugLogger interface { | ||
Debugf(format string, args ...any) | ||
Warnf(format string, args ...any) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package auth | ||
|
||
import "net/http" | ||
|
||
type authorizationChecker interface { | ||
equal(other authorizationChecker) bool | ||
isAuthorized(writer http.ResponseWriter, request *http.Request) bool | ||
} |
Oops, something went wrong.