Skip to content
This repository was archived by the owner on Nov 20, 2021. It is now read-only.

Commit 674e9d0

Browse files
committed
Used oauth for the directive starting oauth and added scaladoc for directives
1 parent b29ea36 commit 674e9d0

File tree

1 file changed

+51
-1
lines changed

1 file changed

+51
-1
lines changed

auth/src/main/scala/com/ulasakdeniz/auth/oauth1/OAuth1Directives.scala

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,59 @@ import akka.http.scaladsl.util.FastFuture._
99
import scala.concurrent.Future
1010

1111
trait OAuth1Directives {
12+
/**
13+
* Defines [[akka.actor.ActorSystem]], [[akka.stream.ActorMaterializer]] and [[OAuthParams]].
14+
*/
1215
val oauthContext: OAuthContext
16+
1317
private[oauth1] lazy val oauthClient = new OAuthClient(oauthContext)
1418

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] = {
1634
val oauthResponseF = oauthClient.requestToken
1735
onSuccess(oauthResponseF)
1836
}
1937

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+
*/
2050
def oauthCallback(tokenProvider: String => Tokens): Directive1[AccessTokenResponse] =
2151
oauthCallbackAsync(token => FastFuture.successful(tokenProvider(token)))
2252

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+
*/
2365
def oauthCallbackAsync(tokenProvider: String => Future[Tokens]): Directive1[AccessTokenResponse] = {
2466
import oauthContext.system.dispatcher
2567
parameters('oauth_token, 'oauth_verifier).tflatMap {
@@ -35,6 +77,14 @@ trait OAuth1Directives {
3577
}
3678

3779
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+
*/
3888
def withAuthorizationHeader(token: String, tokenSecret: String): HttpRequest =
3989
oauthClient.authorizeRequest(httpRequest, token, tokenSecret)
4090
}

0 commit comments

Comments
 (0)