Hi Johan!

Go figure it would be you to bail me out, just like in the good old 
Plotagon days! :)

However this time I couldn't get it compiling with your suggestion. Fails 
with the same error. I got it working in another way though:

trait Microservice {

  import directives.AuthDirectives._

  private def parseScopes(scopeStr: String): Set[OAuthScope] =
    scopeStr
      .split(" ")
      .flatMap(scopeFromString)
      .toSet

  def verifyScopes(required: Set[OAuthScope]): Directive1[AuthHeaders] =
    parseAuthHeaders(parseScopes).flatMap {
      case headers if OAuthScope.hasScopes(required, headers.scopes) => 
provide(headers)
      case _ => reject(AuthorizationFailedRejection)
    }

  /**
    * Each service has it own set of scopes, which we know nothing about at 
this point
    */
  protected def scopeFromString(str: String): Option[OAuthScope]

}




On Thursday, January 7, 2016 at 2:36:55 PM UTC+1, Johan Andrén wrote:
>
> Hi Durre,
>
> Marking a method implicit like that makes it available to do implicit 
> conversion, which means that if 
> there is some code where you have a String and need a Set[OAuthScope] the 
> compiler
> will insert a call to the method to convert types. This is often confusing 
> and makes the code a bit
> hard to read, something to be careful with.
>
> What you are looking for is an implicit value that is to be passed to 
> methods with implicit paremeters
> when not explicitly provided. The compiler will try to find some implicit 
> value that matches the type
> in the current scope, so as your method takes a function:
>
> String => Set[OAuthScope]
>
> you need a value of that type to be available in the scope. You can 
> provide that like this for example:
>
> implicit val parseScopes: String => Set[OAuthScope] = str => str.split(" 
> ").flatMap(scopeFromString).toSet
>
> Good luck!
> --
> Johan Andrén
> Typesafe -  Reactive apps on the JVM
> Twitter: @apnylle
>
> On Thursday, January 7, 2016 at 11:25:53 AM UTC+1, durre wrote:
>>
>> This is probably more of a generic scala question, but I figured the very 
>> best scala developers are clearly found in the akka mailing list, right? :)
>>
>> I would like my verifyScopes directive to magically find my parseScopes 
>> function without me having to explicitly pass it as a parameter. If I do 
>> pass it, it works. If I dont it fails with:
>>
>> [error]  found   : akka.http.scaladsl.server.StandardRoute
>> [error]  required: Set[models.OAuthScope]
>> [error]         complete("All good")
>>
>>
>>
>>
>> implicit def parseScopes(scopeStr: String): Set[OAuthScope] =
>>     scopeStr
>>       .split(" ")
>>       .flatMap(scopeFromString)
>>       .toSet
>>
>>   def parseAuthHeaders(parseScopes: String => Set[OAuthScope]) =
>>     authHeaders.tmap {
>>       case (userId, orgId, scopes) =>
>>         AuthHeaders(
>>           userId = UUID.fromString(userId),
>>           orgId = UUID.fromString(orgId),
>>           scopes = parseScopes(scopes)
>>         )
>>     }
>>
>>
>>   def verifyScopes(required: Set[OAuthScope])(implicit parser: String => 
>> Set[OAuthScope]): Directive1[AuthHeaders] =
>>     parseAuthHeaders(parseScopes).flatMap {
>>       case headers if OAuthScope.hasScopes(required, headers.scopes) => 
>> provide(headers)
>>       case _ => reject(AuthorizationFailedRejection)
>>     }
>>
>>
>>
>>   val route =
>>     get {
>>
>>       verifyScopes(Set(MyScope)) { headers =>
>>         complete("All good")
>>       }
>>     }
>>
>>
>>

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to