@@ -9,17 +9,59 @@ import akka.http.scaladsl.util.FastFuture._
9
9
import scala .concurrent .Future
10
10
11
11
trait OAuth1Directives {
12
+ /**
13
+ * Defines [[akka.actor.ActorSystem ]], [[akka.stream.ActorMaterializer ]] and [[OAuthParams ]].
14
+ */
12
15
val oauthContext : OAuthContext
16
+
13
17
private [oauth1] lazy val oauthClient = new OAuthClient (oauthContext)
14
18
15
- def authenticateOAuth : Directive1 [RequestTokenResponse ] = {
19
+ /**
20
+ * Directive that makes Request Token call to Service Provider.
21
+ *
22
+ * @see https://oauth.net/core/1.0/#auth_step1
23
+ * The Consumer obtains an unauthorized Request Token by asking the Service Provider to issue a Token.
24
+ * The Request Token’s sole purpose is to receive User approval and can only be used to obtain an Access Token.
25
+ *
26
+ * @return [[RequestTokenResponse ]]:
27
+ * - [[RedirectionSuccess ]] includes a redirecting (Found) [[akka.http.scaladsl.model.HttpResponse ]] that can
28
+ * be used to complete request for getting access token. [[RedirectionSuccess ]] also has temporary tokens
29
+ * that should be cached to be able to provide later in [[oauthCallback ]] or [[oauthCallbackAsync ]].
30
+ * - [[RequestTokenFailed ]] includes a failed [[akka.http.scaladsl.model.HttpResponse ]] retrieved from
31
+ * Request Token call.
32
+ */
33
+ def oauth : Directive1 [RequestTokenResponse ] = {
16
34
val oauthResponseF = oauthClient.requestToken
17
35
onSuccess(oauthResponseF)
18
36
}
19
37
38
+ /**
39
+ * Directive to handle when Service Provider directs user back.
40
+ *
41
+ * @see https://oauth.net/core/1.0/#auth_step2 '6.2.3. Service Provider Directs the User Back to the Consumer' part.
42
+ * @param tokenProvider Given `oauth_token` returns tokens that are retrieved in Request Token phase
43
+ * ([[oauth ]] directive).
44
+ * @return [[AccessTokenResponse ]]:
45
+ * - [[AccessTokenSuccess ]] includes Access Tokens granted by Services Provider. These tokens are
46
+ * `oauth_token`, `oauth_token_secret` and additional parameters defined by Service Provider.
47
+ * - [[AccessTokenFailed ]] includes a failed [[akka.http.scaladsl.model.HttpResponse ]] retrieved from
48
+ * Access Token call.
49
+ */
20
50
def oauthCallback (tokenProvider : String => Tokens ): Directive1 [AccessTokenResponse ] =
21
51
oauthCallbackAsync(token => FastFuture .successful(tokenProvider(token)))
22
52
53
+ /**
54
+ * Directive to handle when Service Provider directs user back with an asynchronous `tokenProvider`.
55
+ *
56
+ * @see https://oauth.net/core/1.0/#auth_step2 '6.2.3. Service Provider Directs the User Back to the Consumer' part.
57
+ * @param tokenProvider Given `oauth_token` returns tokens that are retrieved in Request Token phase
58
+ * ([[oauth ]] directive).
59
+ * @return [[AccessTokenResponse ]]:
60
+ * - [[AccessTokenSuccess ]] includes Access Tokens granted by Services Provider. These tokens are
61
+ * `oauth_token`, `oauth_token_secret` and additional parameters defined by Service Provider.
62
+ * - [[AccessTokenFailed ]] includes a failed [[akka.http.scaladsl.model.HttpResponse ]] retrieved from
63
+ * Access Token call.
64
+ */
23
65
def oauthCallbackAsync (tokenProvider : String => Future [Tokens ]): Directive1 [AccessTokenResponse ] = {
24
66
import oauthContext .system .dispatcher
25
67
parameters(' oauth_token , ' oauth_verifier ).tflatMap {
@@ -35,6 +77,14 @@ trait OAuth1Directives {
35
77
}
36
78
37
79
implicit class HttpRequestAuthentication (httpRequest : HttpRequest ) {
80
+ /**
81
+ * Adds OAuth Authorization header given request.
82
+ *
83
+ * @param token `oauth_token` for User.
84
+ * @param tokenSecret `oauth_token_secret` for User.
85
+ *
86
+ * @return [[HttpRequest ]] with OAuth [[akka.http.scaladsl.model.headers.Authorization ]] header.
87
+ */
38
88
def withAuthorizationHeader (token : String , tokenSecret : String ): HttpRequest =
39
89
oauthClient.authorizeRequest(httpRequest, token, tokenSecret)
40
90
}
0 commit comments