Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
I
invinoveritas
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
gbe
invinoveritas
Commits
87419b8f
Commit
87419b8f
authored
3 years ago
by
gbe
Browse files
Options
Downloads
Patches
Plain Diff
Load users from DB
parent
5d17134f
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
auth/auth.go
+15
-2
15 additions, 2 deletions
auth/auth.go
main.go
+33
-6
33 additions, 6 deletions
main.go
migrations/0003-users.sql
+5
-0
5 additions, 0 deletions
migrations/0003-users.sql
with
53 additions
and
8 deletions
auth/auth.go
+
15
−
2
View file @
87419b8f
...
@@ -2,6 +2,8 @@ package auth
...
@@ -2,6 +2,8 @@ package auth
import
(
import
(
"context"
"context"
"fmt"
"log"
"net/http"
"net/http"
)
)
...
@@ -9,7 +11,7 @@ type contextKey string
...
@@ -9,7 +11,7 @@ type contextKey string
type
Provider
interface
{
type
Provider
interface
{
// Returns true if pass is a valid password for the given user
// Returns true if pass is a valid password for the given user
Valid
(
user
,
pass
string
)
bool
Valid
(
ctx
context
.
Context
,
user
,
pass
string
)
(
bool
,
error
)
}
}
// Require wraps hdlr so that it requires authentication to use. Requests handled by hdlr will have the user
// Require wraps hdlr so that it requires authentication to use. Requests handled by hdlr will have the user
...
@@ -24,7 +26,18 @@ func Require(hdlr http.HandlerFunc, provider Provider) http.HandlerFunc {
...
@@ -24,7 +26,18 @@ func Require(hdlr http.HandlerFunc, provider Provider) http.HandlerFunc {
return
return
}
}
if
!
provider
.
Valid
(
user
,
pass
)
{
valid
,
err
:=
provider
.
Valid
(
r
.
Context
(),
user
,
pass
)
if
err
!=
nil
{
log
.
Printf
(
"can't authenticate %s %s from %s: %s"
,
r
.
Method
,
r
.
URL
,
r
.
RemoteAddr
,
err
)
w
.
WriteHeader
(
http
.
StatusInternalServerError
)
fmt
.
Fprintln
(
w
,
"can't confirm your authentication"
)
return
}
if
!
valid
{
w
.
Header
()
.
Add
(
"WWW-Authenticate"
,
`Basic realm="In Vino Veritas"`
)
w
.
Header
()
.
Add
(
"WWW-Authenticate"
,
`Basic realm="In Vino Veritas"`
)
w
.
WriteHeader
(
http
.
StatusUnauthorized
)
w
.
WriteHeader
(
http
.
StatusUnauthorized
)
...
...
This diff is collapsed.
Click to expand it.
main.go
+
33
−
6
View file @
87419b8f
package
main
package
main
import
(
import
(
"context"
"database/sql"
"embed"
"embed"
"errors"
"log"
"log"
"net/http"
"net/http"
"github.com/Masterminds/squirrel"
"github.com/jmoiron/sqlx"
"github.com/jmoiron/sqlx"
_
"modernc.org/sqlite"
// Imported for side effects: registers DB driver
_
"modernc.org/sqlite"
// Imported for side effects: registers DB driver
...
@@ -31,14 +35,35 @@ func httpError(w http.ResponseWriter, msg string, err error, status int) {
...
@@ -31,14 +35,35 @@ func httpError(w http.ResponseWriter, msg string, err error, status int) {
http
.
Error
(
w
,
msg
,
status
)
http
.
Error
(
w
,
msg
,
status
)
}
}
type
authProvider
struct
{}
type
authProvider
struct
{
db
*
sqlx
.
DB
}
func
(
a
authProvider
)
Valid
(
ctx
context
.
Context
,
user
,
pass
string
)
(
bool
,
error
)
{
query
,
args
,
err
:=
squirrel
.
Select
(
"password"
)
.
From
(
"users"
)
.
Where
(
squirrel
.
Eq
{
"name"
:
user
})
.
ToSql
()
if
err
!=
nil
{
return
false
,
err
}
var
dbPass
string
err
=
a
.
db
.
GetContext
(
ctx
,
&
dbPass
,
query
,
args
...
)
if
errors
.
Is
(
err
,
sql
.
ErrNoRows
)
{
// User not found isn't an error, it's just an invalid auth.
return
false
,
nil
}
if
err
!=
nil
{
return
false
,
err
}
func
(
a
authProvider
)
Valid
(
user
,
pass
string
)
bool
{
if
dbPass
==
pass
{
if
user
==
"wine"
&&
pass
==
"potatoe"
{
return
true
,
nil
return
true
}
}
return
false
return
false
,
nil
}
}
func
logRequest
(
r
*
http
.
Request
)
{
func
logRequest
(
r
*
http
.
Request
)
{
...
@@ -65,7 +90,9 @@ func main() {
...
@@ -65,7 +90,9 @@ func main() {
db
:
db
,
db
:
db
,
}
}
ap
:=
authProvider
{}
ap
:=
authProvider
{
db
:
db
,
}
http
.
HandleFunc
(
"/details/img"
,
auth
.
Require
(
http
.
HandlerFunc
(
handler
.
img
),
ap
))
http
.
HandleFunc
(
"/details/img"
,
auth
.
Require
(
http
.
HandlerFunc
(
handler
.
img
),
ap
))
http
.
HandleFunc
(
"/details/"
,
auth
.
Require
(
http
.
HandlerFunc
(
handler
.
details
),
ap
))
http
.
HandleFunc
(
"/details/"
,
auth
.
Require
(
http
.
HandlerFunc
(
handler
.
details
),
ap
))
...
...
This diff is collapsed.
Click to expand it.
migrations/0003-users.sql
0 → 100644
+
5
−
0
View file @
87419b8f
CREATE
TABLE
users
(
name
TEXT
,
password
TEXT
,
UNIQUE
(
name
)
);
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment