-
Notifications
You must be signed in to change notification settings - Fork 0
feat: add api authentication #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
7036f2f
refactor: move authentication into separate package
eynopv 0f7ceb7
feat: add api authentication
eynopv 749d8f7
feat: add api authentication into factory
eynopv fc1b40f
fix: add yaml token field
eynopv 1e4a0e1
test: remove trailing comma from json
eynopv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,47 @@ | ||
| package authentication | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "github.com/eynopv/lac/internal/errorsx" | ||
| "github.com/eynopv/lac/pkg/request" | ||
| "gopkg.in/yaml.v3" | ||
| ) | ||
|
|
||
| type ApiAuth struct { | ||
| Header string `json:"header" yaml:"header"` | ||
| Key string `json:"key" yaml:"key"` | ||
| } | ||
|
|
||
| func NewApiAuth(t *request.Template) (*ApiAuth, error) { | ||
| var wrapper struct { | ||
| Auth *ApiAuth `json:"auth" yaml:"auth"` | ||
| } | ||
|
|
||
| bs := []byte(*t) | ||
|
|
||
| err := json.Unmarshal(bs, &wrapper) | ||
| if err != nil { | ||
| err = yaml.Unmarshal(bs, &wrapper) | ||
| } | ||
|
|
||
| if err != nil { | ||
| return nil, fmt.Errorf("%w: %v", errorsx.ErrApiAuthParse, err) | ||
| } | ||
|
|
||
| if wrapper.Auth != nil && (wrapper.Auth.Header == "" || wrapper.Auth.Key == "") { | ||
| return nil, errorsx.ErrApiAuthInvalid | ||
| } | ||
|
|
||
| return wrapper.Auth, nil | ||
| } | ||
|
|
||
| func (a *ApiAuth) Apply(r *http.Request) { | ||
| r.Header.Set(a.Header, a.Key) | ||
| } | ||
|
|
||
| func (a *ApiAuth) GetType() AuthType { | ||
| return Api | ||
| } |
This file contains hidden or 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,82 @@ | ||
| package authentication | ||
|
|
||
| import ( | ||
| "net/http" | ||
| "testing" | ||
|
|
||
| "github.com/eynopv/lac/internal/assert" | ||
| "github.com/eynopv/lac/pkg/request" | ||
| ) | ||
|
|
||
| func TestNewApiAuth(t *testing.T) { | ||
| t.Run("yaml", func(t *testing.T) { | ||
| template := request.Template(` | ||
| auth: | ||
| header: x-api-key | ||
| key: helloworld | ||
| `) | ||
|
|
||
| auth, err := NewApiAuth(&template) | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.NotNil(t, auth) | ||
| assert.Equal(t, auth.Header, "x-api-key") | ||
| assert.Equal(t, auth.Key, "helloworld") | ||
| }) | ||
|
|
||
| t.Run("json", func(t *testing.T) { | ||
| template := request.Template(` | ||
| { | ||
| "auth": { | ||
| "header": "x-api-key", | ||
| "key": "helloworld" | ||
| } | ||
| } | ||
| `) | ||
|
|
||
| auth, err := NewApiAuth(&template) | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.NotNil(t, auth) | ||
| assert.Equal(t, auth.Header, "x-api-key") | ||
| assert.Equal(t, auth.Key, "helloworld") | ||
| }) | ||
|
|
||
| t.Run("invalid", func(t *testing.T) { | ||
| template := request.Template("this is invalid template") | ||
|
|
||
| auth, err := NewApiAuth(&template) | ||
|
|
||
| assert.Error(t, err) | ||
| assert.Nil(t, auth) | ||
| }) | ||
|
|
||
| t.Run("template without auth", func(t *testing.T) { | ||
| template := request.Template(` | ||
| { | ||
| "hello": "world" | ||
| } | ||
| `) | ||
|
|
||
| auth, err := NewApiAuth(&template) | ||
|
|
||
| assert.NoError(t, err) | ||
| assert.True(t, auth == nil) | ||
| }) | ||
| } | ||
|
|
||
| func TestApiAuthApply(t *testing.T) { | ||
| apiAuth := ApiAuth{ | ||
| Header: "x-api-key", | ||
| Key: "helloworld", | ||
| } | ||
|
|
||
| request, err := http.NewRequest(http.MethodGet, "", nil) | ||
|
|
||
| assert.NotNil(t, request) | ||
| assert.NoError(t, err) | ||
|
|
||
| apiAuth.Apply(request) | ||
|
|
||
| assert.Equal(t, request.Header.Get("X-Api-Key"), "helloworld") | ||
| } |
This file contains hidden or 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,59 @@ | ||
| package authentication | ||
|
|
||
| import ( | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
|
|
||
| "gopkg.in/yaml.v3" | ||
|
|
||
| "github.com/eynopv/lac/internal/errorsx" | ||
| "github.com/eynopv/lac/pkg/request" | ||
| ) | ||
|
|
||
| type AuthType string | ||
|
|
||
| const ( | ||
| Api AuthType = "api" | ||
| Basic AuthType = "basic" | ||
| Bearer AuthType = "bearer" | ||
| ) | ||
|
|
||
| type Auth interface { | ||
| Apply(r *http.Request) | ||
| GetType() AuthType | ||
| } | ||
|
|
||
| type AuthBase struct { | ||
| Type AuthType `json:"type" yaml:"type"` | ||
| } | ||
|
|
||
| func NewAuth(t *request.Template) (Auth, error) { | ||
| var wrapper struct { | ||
| Auth *AuthBase `json:"auth" yaml:"auth"` | ||
| } | ||
|
|
||
| bs := []byte(*t) | ||
|
|
||
| err := json.Unmarshal(bs, &wrapper) | ||
| if err != nil { | ||
| if err = yaml.Unmarshal(bs, &wrapper); err != nil { | ||
| return nil, fmt.Errorf("%w: %v", errorsx.ErrAuthParse, err) | ||
| } | ||
| } | ||
|
|
||
| if wrapper.Auth == nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| switch wrapper.Auth.Type { | ||
| case Api: | ||
| return NewApiAuth(t) | ||
| case Basic: | ||
| return NewBasicAuth(t) | ||
| case Bearer: | ||
| return NewBearerAuth(t) | ||
| default: | ||
| return nil, errorsx.ErrAuthUnknown | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.