Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
dex
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
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
hdacloud
dex
Commits
cdf0b916
Commit
cdf0b916
authored
8 years ago
by
Eric Chiang
Browse files
Options
Downloads
Patches
Plain Diff
server: add an option to enable emails and passwords from the database
parent
7ff3ce85
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
server/server.go
+45
-0
45 additions, 0 deletions
server/server.go
server/server_test.go
+88
-0
88 additions, 0 deletions
server/server_test.go
with
133 additions
and
0 deletions
server/server.go
+
45
−
0
View file @
cdf0b916
...
@@ -3,12 +3,15 @@ package server
...
@@ -3,12 +3,15 @@ package server
import
(
import
(
"errors"
"errors"
"fmt"
"fmt"
"log"
"net/http"
"net/http"
"net/url"
"net/url"
"path"
"path"
"sync/atomic"
"sync/atomic"
"time"
"time"
"golang.org/x/crypto/bcrypt"
"github.com/gorilla/mux"
"github.com/gorilla/mux"
"github.com/coreos/dex/connector"
"github.com/coreos/dex/connector"
...
@@ -44,6 +47,8 @@ type Config struct {
...
@@ -44,6 +47,8 @@ type Config struct {
// If specified, the server will use this function for determining time.
// If specified, the server will use this function for determining time.
Now
func
()
time
.
Time
Now
func
()
time
.
Time
EnablePasswordDB
bool
TemplateConfig
TemplateConfig
TemplateConfig
TemplateConfig
}
}
...
@@ -91,6 +96,14 @@ func newServer(c Config, rotationStrategy rotationStrategy) (*Server, error) {
...
@@ -91,6 +96,14 @@ func newServer(c Config, rotationStrategy rotationStrategy) (*Server, error) {
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
fmt
.
Errorf
(
"server: can't parse issuer URL"
)
return
nil
,
fmt
.
Errorf
(
"server: can't parse issuer URL"
)
}
}
if
c
.
EnablePasswordDB
{
c
.
Connectors
=
append
(
c
.
Connectors
,
Connector
{
ID
:
"local"
,
DisplayName
:
"Email"
,
Connector
:
newPasswordDB
(
c
.
Storage
),
})
}
if
len
(
c
.
Connectors
)
==
0
{
if
len
(
c
.
Connectors
)
==
0
{
return
nil
,
errors
.
New
(
"server: no connectors specified"
)
return
nil
,
errors
.
New
(
"server: no connectors specified"
)
}
}
...
@@ -182,6 +195,38 @@ func (s *Server) absURL(pathItems ...string) string {
...
@@ -182,6 +195,38 @@ func (s *Server) absURL(pathItems ...string) string {
return
u
.
String
()
return
u
.
String
()
}
}
func
newPasswordDB
(
s
storage
.
Storage
)
interface
{
connector
.
Connector
connector
.
PasswordConnector
}
{
return
passwordDB
{
s
}
}
type
passwordDB
struct
{
s
storage
.
Storage
}
func
(
db
passwordDB
)
Close
()
error
{
return
nil
}
func
(
db
passwordDB
)
Login
(
email
,
password
string
)
(
connector
.
Identity
,
bool
,
error
)
{
p
,
err
:=
db
.
s
.
GetPassword
(
email
)
if
err
!=
nil
{
if
err
!=
storage
.
ErrNotFound
{
log
.
Printf
(
"get password: %v"
,
err
)
}
return
connector
.
Identity
{},
false
,
err
}
if
err
:=
bcrypt
.
CompareHashAndPassword
(
p
.
Hash
,
[]
byte
(
password
));
err
!=
nil
{
return
connector
.
Identity
{},
false
,
nil
}
return
connector
.
Identity
{
UserID
:
p
.
UserID
,
Username
:
p
.
Username
,
Email
:
p
.
Email
,
EmailVerified
:
true
,
},
true
,
nil
}
// newKeyCacher returns a storage which caches keys so long as the next
// newKeyCacher returns a storage which caches keys so long as the next
func
newKeyCacher
(
s
storage
.
Storage
,
now
func
()
time
.
Time
)
storage
.
Storage
{
func
newKeyCacher
(
s
storage
.
Storage
,
now
func
()
time
.
Time
)
storage
.
Storage
{
if
now
==
nil
{
if
now
==
nil
{
...
...
This diff is collapsed.
Click to expand it.
server/server_test.go
+
88
−
0
View file @
cdf0b916
...
@@ -16,9 +16,12 @@ import (
...
@@ -16,9 +16,12 @@ import (
"time"
"time"
"github.com/ericchiang/oidc"
"github.com/ericchiang/oidc"
"github.com/kylelemons/godebug/pretty"
"golang.org/x/crypto/bcrypt"
"golang.org/x/net/context"
"golang.org/x/net/context"
"golang.org/x/oauth2"
"golang.org/x/oauth2"
"github.com/coreos/dex/connector"
"github.com/coreos/dex/connector/mock"
"github.com/coreos/dex/connector/mock"
"github.com/coreos/dex/storage"
"github.com/coreos/dex/storage"
"github.com/coreos/dex/storage/memory"
"github.com/coreos/dex/storage/memory"
...
@@ -381,6 +384,91 @@ func TestOAuth2ImplicitFlow(t *testing.T) {
...
@@ -381,6 +384,91 @@ func TestOAuth2ImplicitFlow(t *testing.T) {
}
}
}
}
func
TestPasswordDB
(
t
*
testing
.
T
)
{
s
:=
memory
.
New
()
conn
:=
newPasswordDB
(
s
)
defer
conn
.
Close
()
pw
:=
"hi"
h
,
err
:=
bcrypt
.
GenerateFromPassword
([]
byte
(
pw
),
bcrypt
.
MinCost
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
s
.
CreatePassword
(
storage
.
Password
{
Email
:
"jane@example.com"
,
Username
:
"jane"
,
UserID
:
"foobar"
,
Hash
:
h
,
})
tests
:=
[]
struct
{
name
string
username
string
password
string
wantIdentity
connector
.
Identity
wantInvalid
bool
wantErr
bool
}{
{
name
:
"valid password"
,
username
:
"jane@example.com"
,
password
:
pw
,
wantIdentity
:
connector
.
Identity
{
Email
:
"jane@example.com"
,
Username
:
"jane"
,
UserID
:
"foobar"
,
EmailVerified
:
true
,
},
},
{
name
:
"unknown user"
,
username
:
"john@example.com"
,
password
:
pw
,
wantErr
:
true
,
},
{
name
:
"invalid password"
,
username
:
"jane@example.com"
,
password
:
"not the correct password"
,
wantInvalid
:
true
,
},
}
for
_
,
tc
:=
range
tests
{
ident
,
valid
,
err
:=
conn
.
Login
(
tc
.
username
,
tc
.
password
)
if
err
!=
nil
{
if
!
tc
.
wantErr
{
t
.
Errorf
(
"%s: %v"
,
tc
.
name
,
err
)
}
continue
}
if
tc
.
wantErr
{
t
.
Errorf
(
"%s: expected error"
,
tc
.
name
)
continue
}
if
!
valid
{
if
!
tc
.
wantInvalid
{
t
.
Errorf
(
"%s: expected valid password"
,
tc
.
name
)
}
continue
}
if
tc
.
wantInvalid
{
t
.
Errorf
(
"%s: expected invalid password"
,
tc
.
name
)
continue
}
if
diff
:=
pretty
.
Compare
(
tc
.
wantIdentity
,
ident
);
diff
!=
""
{
t
.
Errorf
(
"%s: %s"
,
tc
.
name
,
diff
)
}
}
}
type
storageWithKeysTrigger
struct
{
type
storageWithKeysTrigger
struct
{
storage
.
Storage
storage
.
Storage
f
func
()
f
func
()
...
...
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