Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
V
vess-http-database
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD 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
joel.cavat
vess-http-database
Commits
015811cb
Commit
015811cb
authored
6 years ago
by
Joel Cavat
Browse files
Options
Downloads
Patches
Plain Diff
Example with bearer tokens
parent
804e54fb
No related branches found
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
README.md
+6
-0
6 additions, 0 deletions
README.md
src/main/resources/application.conf
+1
-0
1 addition, 0 deletions
src/main/resources/application.conf
src/main/scala/com/example/UserRoutes.scala
+9
-3
9 additions, 3 deletions
src/main/scala/com/example/UserRoutes.scala
with
16 additions
and
3 deletions
README.md
0 → 100644
+
6
−
0
View file @
015811cb
```
curl -H "Authorization: Bearer ABCD" http://localhost:8080/users
```
This diff is collapsed.
Click to expand it.
src/main/resources/application.conf
0 → 100644
+
1
−
0
View file @
015811cb
tokens
= [
"ABCD"
,
"DEFG"
]
\ No newline at end of file
This diff is collapsed.
Click to expand it.
src/main/scala/com/example/UserRoutes.scala
+
9
−
3
View file @
015811cb
...
@@ -20,6 +20,9 @@ import com.example.UserRegistryActor._
...
@@ -20,6 +20,9 @@ import com.example.UserRegistryActor._
import
akka.pattern.ask
import
akka.pattern.ask
import
akka.util.Timeout
import
akka.util.Timeout
import
com.typesafe.config.ConfigFactory
import
collection.JavaConversions._
//#user-routes-class
//#user-routes-class
trait
UserRoutes
extends
JsonSupport
{
trait
UserRoutes
extends
JsonSupport
{
//#user-routes-class
//#user-routes-class
...
@@ -35,8 +38,10 @@ trait UserRoutes extends JsonSupport {
...
@@ -35,8 +38,10 @@ trait UserRoutes extends JsonSupport {
// Required by the `ask` (?) method below
// Required by the `ask` (?) method below
implicit
lazy
val
timeout
=
Timeout
(
5.
seconds
)
// usually we'd obtain the timeout from the system's configuration
implicit
lazy
val
timeout
=
Timeout
(
5.
seconds
)
// usually we'd obtain the timeout from the system's configuration
val
tokens
:
List
[
String
]
=
ConfigFactory
.
load
().
getStringList
(
"tokens"
).
toList
def
check
(
credentials
:
Credentials
)
:
Option
[
String
]
=
credentials
match
{
def
check
(
credentials
:
Credentials
)
:
Option
[
String
]
=
credentials
match
{
case
p
@
Credentials
.
Provided
(
id
)
if
id
==
"john"
&&
p
.
verify
(
"p4ssw0rd"
)
=>
Some
(
id
)
case
p
@
Credentials
.
Provided
(
token
)
if
tokens
.
exists
(
t
=>
p
.
verify
(
t
)
)
=>
Some
(
token
)
case
_
=>
None
case
_
=>
None
}
}
...
@@ -45,7 +50,8 @@ trait UserRoutes extends JsonSupport {
...
@@ -45,7 +50,8 @@ trait UserRoutes extends JsonSupport {
//#users-get-delete
//#users-get-delete
lazy
val
userRoutes
:
Route
=
Route
.
seal
{
lazy
val
userRoutes
:
Route
=
Route
.
seal
{
pathPrefix
(
"users"
)
{
pathPrefix
(
"users"
)
{
authenticateBasic
(
realm
=
"secure site"
,
check
)
{
userName
=>
//authenticateBasic(realm = "secure site", check) { userName =>
authenticateOAuth2
(
realm
=
"secure site"
,
check
)
{
token
=>
concat
(
concat
(
//#users-get-delete
//#users-get-delete
pathEnd
{
pathEnd
{
...
@@ -53,7 +59,7 @@ trait UserRoutes extends JsonSupport {
...
@@ -53,7 +59,7 @@ trait UserRoutes extends JsonSupport {
get
{
get
{
val
users
:
Future
[
Users
]
=
val
users
:
Future
[
Users
]
=
(
userRegistryActor
?
GetUsers
).
mapTo
[
Users
]
(
userRegistryActor
?
GetUsers
).
mapTo
[
Users
]
log
.
info
(
userName
+
" registered"
)
log
.
info
(
token
+
" registered"
)
complete
(
users
)
complete
(
users
)
},
},
post
{
post
{
...
...
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