-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from itishrishikesh/auth
custom authentication mechanism added
- Loading branch information
Showing
8 changed files
with
103 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package auth | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
// GetAPIKey extracts an API key from the header of an HTTP request | ||
// Example: | ||
// Authorization: ApiKey {insert apikey here} | ||
func GetAPIKey(header http.Header) (string, error) { | ||
val := header.Get("Authorization") | ||
if val == "" { | ||
return "", errors.New("E#1OTELT - No authentication info found!") | ||
} | ||
vals := strings.Split(val, " ") | ||
if len(vals) != 2 { | ||
return "", errors.New("E#1OTEM0 - Malformed auth header") | ||
} | ||
if vals[0] != "ApiKey" { | ||
return "", errors.New("E#1OTEM5 - Malformed auth header") | ||
} | ||
return vals[1], nil | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,26 @@ | ||
package main | ||
|
||
import ( | ||
"time" | ||
|
||
"github.com/google/uuid" | ||
"github.com/itishrishikesh/gofeed/internal/database" | ||
) | ||
|
||
type User struct { | ||
ID uuid.UUID `json:"id"` | ||
CreatedAt time.Time `json:"created_at"` | ||
UpdatedAt time.Time `json:"updated_at"` | ||
Name string `json:"name"` | ||
ApiKey string `json:"api_key"` | ||
} | ||
|
||
func databaseUserToUser(dbUser database.User) User { | ||
return User{ | ||
ID: dbUser.ID, | ||
CreatedAt: dbUser.CreatedAt, | ||
UpdatedAt: dbUser.UpdatedAt, | ||
Name: dbUser.Name, | ||
ApiKey: dbUser.ApiKey, | ||
} | ||
} |
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 |
---|---|---|
@@ -1,4 +1,7 @@ | ||
-- name: CreateUser :one | ||
INSERT INTO users (id, created_at, updated_at, name) | ||
VALUES ($1, $2, $3, $4) | ||
RETURNING *; | ||
INSERT INTO users (id, created_at, updated_at, name, api_key) | ||
VALUES ($1, $2, $3, $4, encode(sha256(random()::text::bytea), 'hex')) | ||
RETURNING *; | ||
|
||
-- name: GetUserByAPIKey :one | ||
SELECT * FROM users WHERE api_key = $1; |
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,7 @@ | ||
-- +goose Up | ||
ALTER TABLE users ADD COLUMN api_key VARCHAR(64) UNIQUE NOT NULL DEFAULT ( | ||
encode(sha256(random()::text::bytea), 'hex') | ||
); | ||
|
||
-- +goose Down | ||
ALTER TABLE users DROP COLUMN api_key; |
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