1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.api.technical.directives
21 
22 import akka.http.scaladsl.model.headers.{Authorization, BasicHttpCredentials}
23 import akka.http.scaladsl.server.Directive1
24 import org.make.api.technical.auth.ClientServiceComponent
25 import org.make.api.technical.directives.FutureDirectivesExtensions._
26 import org.make.core.auth.{Client, ClientId}
27 
28 trait ClientDirectives { self: ClientServiceComponent =>
29 
30   def extractClient: Directive1[Option[Client]] = {
31     optionalHeaderValueByType(Authorization).flatMap {
32       case Some(Authorization(BasicHttpCredentials(clientId, clientSecret))) =>
33         val secret = clientSecret match {
34           case ""    => None
35           case other => Some(other)
36         }
37         clientService.getClient(ClientId(clientId), secret).asDirective.flatMap {
38           case Left(e)       => failWith(e)
39           case Right(client) => provide(Some(client))
40         }
41       case _ => provide[Option[Client]](None)
42     }
43   }
44 
45   def extractClientOrDefault: Directive1[Client] = {
46     extractClient.flatMap {
47       case Some(client) => provide(client)
48       case None =>
49         clientService.getDefaultClient().asDirective.flatMap {
50           case Left(e)       => failWith(e)
51           case Right(client) => provide(client)
52         }
53     }
54   }
55 }
Line Stmt Id Pos Tree Symbol Tests Code
31 39233 1224 - 1237 ApplyToImplicitArgs akka.http.scaladsl.server.directives.LowPriorityHeaderMagnetImplicits.fromCompanionNormalHeader org.make.api.technical.directives.clientdirectivestest directives.this.HeaderMagnet.fromCompanionNormalHeader[akka.http.scaladsl.model.headers.Authorization](akka.http.scaladsl.model.headers.Authorization)((ClassTag.apply[akka.http.scaladsl.model.headers.Authorization](classOf[akka.http.scaladsl.model.headers.Authorization]): scala.reflect.ClassTag[akka.http.scaladsl.model.headers.Authorization]))
31 42310 1198 - 1687 ApplyToImplicitArgs akka.http.scaladsl.server.Directive.SingleValueTransformers.flatMap org.make.api.technical.directives.clientdirectivestest server.this.Directive.SingleValueTransformers[Option[akka.http.scaladsl.model.headers.Authorization]](org.make.api.technical.directives.FutureDirectivesExtensions.optionalHeaderValueByType[akka.http.scaladsl.model.headers.Authorization](directives.this.HeaderMagnet.fromCompanionNormalHeader[akka.http.scaladsl.model.headers.Authorization](akka.http.scaladsl.model.headers.Authorization)((ClassTag.apply[akka.http.scaladsl.model.headers.Authorization](classOf[akka.http.scaladsl.model.headers.Authorization]): scala.reflect.ClassTag[akka.http.scaladsl.model.headers.Authorization])))).flatMap[(Option[org.make.core.auth.Client],)](((x0$1: Option[akka.http.scaladsl.model.headers.Authorization]) => x0$1 match { case (value: akka.http.scaladsl.model.headers.Authorization): Some[akka.http.scaladsl.model.headers.Authorization]((credentials: akka.http.scaladsl.model.headers.HttpCredentials): akka.http.scaladsl.model.headers.Authorization((username: String, password: String): akka.http.scaladsl.model.headers.BasicHttpCredentials((clientId @ _), (clientSecret @ _)))) => { val secret: Option[String] = clientSecret match { case "" => scala.None case (other @ _) => scala.Some.apply[String](other) }; server.this.Directive.SingleValueTransformers[Either[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]](org.make.api.technical.directives.FutureDirectivesExtensions.FutureWithRoutes[Either[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]](ClientDirectives.this.clientService.getClient(org.make.core.auth.ClientId.apply(clientId), secret)).asDirective).flatMap[(Option[org.make.core.auth.Client],)](((x0$2: Either[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]) => x0$2 match { case (value: scalaoauth2.provider.OAuthError): scala.util.Left[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]((e @ _)) => server.this.StandardRoute.toDirective[(Option[org.make.core.auth.Client],)](org.make.api.technical.directives.FutureDirectivesExtensions.failWith(e))(util.this.Tuple.forTuple1[Option[org.make.core.auth.Client]]) case (value: org.make.core.auth.Client): scala.util.Right[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]((client @ _)) => org.make.api.technical.directives.FutureDirectivesExtensions.provide[Option[org.make.core.auth.Client]](scala.Some.apply[org.make.core.auth.Client](client)) }))(util.this.Tuple.forTuple1[Option[org.make.core.auth.Client]]) } case _ => org.make.api.technical.directives.FutureDirectivesExtensions.provide[Option[org.make.core.auth.Client]](scala.None) }))(util.this.Tuple.forTuple1[Option[org.make.core.auth.Client]])
31 31349 1198 - 1238 Apply akka.http.scaladsl.server.directives.HeaderDirectives.optionalHeaderValueByType org.make.api.technical.directives.clientdirectivestest org.make.api.technical.directives.FutureDirectivesExtensions.optionalHeaderValueByType[akka.http.scaladsl.model.headers.Authorization](directives.this.HeaderMagnet.fromCompanionNormalHeader[akka.http.scaladsl.model.headers.Authorization](akka.http.scaladsl.model.headers.Authorization)((ClassTag.apply[akka.http.scaladsl.model.headers.Authorization](classOf[akka.http.scaladsl.model.headers.Authorization]): scala.reflect.ClassTag[akka.http.scaladsl.model.headers.Authorization])))
31 50431 1247 - 1247 TypeApply akka.http.scaladsl.server.util.Tuple.forTuple1 org.make.api.technical.directives.clientdirectivestest util.this.Tuple.forTuple1[Option[org.make.core.auth.Client]]
31 47350 1224 - 1237 Select akka.http.scaladsl.model.headers.Authorization org.make.api.technical.directives.clientdirectivestest akka.http.scaladsl.model.headers.Authorization
34 44140 1395 - 1399 Select scala.None org.make.api.technical.directives.clientdirectivestest scala.None
35 36895 1424 - 1435 Apply scala.Some.apply org.make.api.technical.directives.clientdirectivestest scala.Some.apply[String](other)
37 32477 1454 - 1635 ApplyToImplicitArgs akka.http.scaladsl.server.Directive.SingleValueTransformers.flatMap org.make.api.technical.directives.clientdirectivestest server.this.Directive.SingleValueTransformers[Either[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]](org.make.api.technical.directives.FutureDirectivesExtensions.FutureWithRoutes[Either[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]](ClientDirectives.this.clientService.getClient(org.make.core.auth.ClientId.apply(clientId), secret)).asDirective).flatMap[(Option[org.make.core.auth.Client],)](((x0$2: Either[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]) => x0$2 match { case (value: scalaoauth2.provider.OAuthError): scala.util.Left[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]((e @ _)) => server.this.StandardRoute.toDirective[(Option[org.make.core.auth.Client],)](org.make.api.technical.directives.FutureDirectivesExtensions.failWith(e))(util.this.Tuple.forTuple1[Option[org.make.core.auth.Client]]) case (value: org.make.core.auth.Client): scala.util.Right[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]((client @ _)) => org.make.api.technical.directives.FutureDirectivesExtensions.provide[Option[org.make.core.auth.Client]](scala.Some.apply[org.make.core.auth.Client](client)) }))(util.this.Tuple.forTuple1[Option[org.make.core.auth.Client]])
37 36338 1526 - 1526 TypeApply akka.http.scaladsl.server.util.Tuple.forTuple1 org.make.api.technical.directives.clientdirectivestest util.this.Tuple.forTuple1[Option[org.make.core.auth.Client]]
37 32438 1478 - 1496 Apply org.make.core.auth.ClientId.apply org.make.api.technical.directives.clientdirectivestest org.make.core.auth.ClientId.apply(clientId)
37 37365 1454 - 1517 Select org.make.api.technical.directives.FutureDirectivesExtensions.FutureWithRoutes.asDirective org.make.api.technical.directives.clientdirectivestest org.make.api.technical.directives.FutureDirectivesExtensions.FutureWithRoutes[Either[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]](ClientDirectives.this.clientService.getClient(org.make.core.auth.ClientId.apply(clientId), secret)).asDirective
37 45765 1454 - 1505 Apply org.make.api.technical.auth.ClientService.getClient org.make.api.technical.directives.clientdirectivestest ClientDirectives.this.clientService.getClient(org.make.core.auth.ClientId.apply(clientId), secret)
38 42869 1568 - 1568 TypeApply akka.http.scaladsl.server.util.Tuple.forTuple1 org.make.api.technical.directives.clientdirectivestest util.this.Tuple.forTuple1[Option[org.make.core.auth.Client]]
38 38992 1560 - 1571 ApplyToImplicitArgs akka.http.scaladsl.server.StandardRoute.toDirective org.make.api.technical.directives.clientdirectivestest server.this.StandardRoute.toDirective[(Option[org.make.core.auth.Client],)](org.make.api.technical.directives.FutureDirectivesExtensions.failWith(e))(util.this.Tuple.forTuple1[Option[org.make.core.auth.Client]])
38 50679 1560 - 1571 Apply akka.http.scaladsl.server.directives.RouteDirectives.failWith org.make.api.technical.directives.clientdirectivestest org.make.api.technical.directives.FutureDirectivesExtensions.failWith(e)
39 43898 1604 - 1625 Apply akka.http.scaladsl.server.directives.BasicDirectives.provide org.make.api.technical.directives.clientdirectivestest org.make.api.technical.directives.FutureDirectivesExtensions.provide[Option[org.make.core.auth.Client]](scala.Some.apply[org.make.core.auth.Client](client))
39 30834 1612 - 1624 Apply scala.Some.apply org.make.api.technical.directives.clientdirectivestest scala.Some.apply[org.make.core.auth.Client](client)
41 37399 1652 - 1681 Apply akka.http.scaladsl.server.directives.BasicDirectives.provide org.make.api.technical.directives.clientdirectivestest org.make.api.technical.directives.FutureDirectivesExtensions.provide[Option[org.make.core.auth.Client]](scala.None)
41 45520 1676 - 1680 Select scala.None org.make.api.technical.directives.clientdirectivestest scala.None
46 43686 1750 - 2006 ApplyToImplicitArgs akka.http.scaladsl.server.Directive.SingleValueTransformers.flatMap org.make.api.technical.directives.clientdirectivestest server.this.Directive.SingleValueTransformers[Option[org.make.core.auth.Client]](ClientDirectives.this.extractClient).flatMap[(org.make.core.auth.Client,)](((x0$1: Option[org.make.core.auth.Client]) => x0$1 match { case (value: org.make.core.auth.Client): Some[org.make.core.auth.Client]((client @ _)) => org.make.api.technical.directives.FutureDirectivesExtensions.provide[org.make.core.auth.Client](client) case scala.None => server.this.Directive.SingleValueTransformers[Either[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]](org.make.api.technical.directives.FutureDirectivesExtensions.FutureWithRoutes[Either[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]](ClientDirectives.this.clientService.getDefaultClient()).asDirective).flatMap[(org.make.core.auth.Client,)](((x0$2: Either[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]) => x0$2 match { case (value: scalaoauth2.provider.OAuthError): scala.util.Left[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]((e @ _)) => server.this.StandardRoute.toDirective[(org.make.core.auth.Client,)](org.make.api.technical.directives.FutureDirectivesExtensions.failWith(e))(util.this.Tuple.forTuple1[org.make.core.auth.Client]) case (value: org.make.core.auth.Client): scala.util.Right[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]((client @ _)) => org.make.api.technical.directives.FutureDirectivesExtensions.provide[org.make.core.auth.Client](client) }))(util.this.Tuple.forTuple1[org.make.core.auth.Client]) }))(util.this.Tuple.forTuple1[org.make.core.auth.Client])
46 30630 1772 - 1772 TypeApply akka.http.scaladsl.server.util.Tuple.forTuple1 org.make.api.technical.directives.clientdirectivestest util.this.Tuple.forTuple1[org.make.core.auth.Client]
46 39027 1750 - 1763 Select org.make.api.technical.directives.ClientDirectives.extractClient org.make.api.technical.directives.clientdirectivestest ClientDirectives.this.extractClient
47 30593 1801 - 1816 Apply akka.http.scaladsl.server.directives.BasicDirectives.provide org.make.api.technical.directives.clientdirectivestest org.make.api.technical.directives.FutureDirectivesExtensions.provide[org.make.core.auth.Client](client)
49 35821 1844 - 1888 Select org.make.api.technical.directives.FutureDirectivesExtensions.FutureWithRoutes.asDirective org.make.api.technical.directives.clientdirectivestest org.make.api.technical.directives.FutureDirectivesExtensions.FutureWithRoutes[Either[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]](ClientDirectives.this.clientService.getDefaultClient()).asDirective
49 39481 1844 - 2000 ApplyToImplicitArgs akka.http.scaladsl.server.Directive.SingleValueTransformers.flatMap org.make.api.technical.directives.clientdirectivestest server.this.Directive.SingleValueTransformers[Either[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]](org.make.api.technical.directives.FutureDirectivesExtensions.FutureWithRoutes[Either[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]](ClientDirectives.this.clientService.getDefaultClient()).asDirective).flatMap[(org.make.core.auth.Client,)](((x0$2: Either[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]) => x0$2 match { case (value: scalaoauth2.provider.OAuthError): scala.util.Left[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]((e @ _)) => server.this.StandardRoute.toDirective[(org.make.core.auth.Client,)](org.make.api.technical.directives.FutureDirectivesExtensions.failWith(e))(util.this.Tuple.forTuple1[org.make.core.auth.Client]) case (value: org.make.core.auth.Client): scala.util.Right[scalaoauth2.provider.OAuthError,org.make.core.auth.Client]((client @ _)) => org.make.api.technical.directives.FutureDirectivesExtensions.provide[org.make.core.auth.Client](client) }))(util.this.Tuple.forTuple1[org.make.core.auth.Client])
49 43933 1844 - 1876 Apply org.make.api.technical.auth.ClientService.getDefaultClient org.make.api.technical.directives.clientdirectivestest ClientDirectives.this.clientService.getDefaultClient()
49 42347 1897 - 1897 TypeApply akka.http.scaladsl.server.util.Tuple.forTuple1 org.make.api.technical.directives.clientdirectivestest util.this.Tuple.forTuple1[org.make.core.auth.Client]
50 37164 1931 - 1942 ApplyToImplicitArgs akka.http.scaladsl.server.StandardRoute.toDirective server.this.StandardRoute.toDirective[(org.make.core.auth.Client,)](org.make.api.technical.directives.FutureDirectivesExtensions.failWith(e))(util.this.Tuple.forTuple1[org.make.core.auth.Client])
50 45002 1939 - 1939 TypeApply akka.http.scaladsl.server.util.Tuple.forTuple1 util.this.Tuple.forTuple1[org.make.core.auth.Client]
50 32943 1931 - 1942 Apply akka.http.scaladsl.server.directives.RouteDirectives.failWith org.make.api.technical.directives.FutureDirectivesExtensions.failWith(e)
51 50473 1975 - 1990 Apply akka.http.scaladsl.server.directives.BasicDirectives.provide org.make.api.technical.directives.clientdirectivestest org.make.api.technical.directives.FutureDirectivesExtensions.provide[org.make.core.auth.Client](client)