Skip to content

Commit f1bd937

Browse files
author
Rafa Paradela
committed
Wip
1 parent 95c0ca6 commit f1bd937

6 files changed

Lines changed: 56 additions & 4 deletions

File tree

src/main/scala/com/fortysevendeg/github4s/GithubAPIs.scala

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,12 @@ class GHAuth(implicit O : AuthOps[GitHub4s]){
4848
redirect_uri: String,
4949
scopes: List[String]): GHIO[GHResponse[Authorize]] =
5050
O.authorizeUrl(client_id, redirect_uri, scopes)
51+
52+
def getAccessToken(
53+
client_id: String,
54+
client_secret: String,
55+
code: String,
56+
redirect_uri: String,
57+
state: String): GHIO[GHResponse[OAuthToken]] =
58+
O.getAccessToken(client_id, client_secret, code, redirect_uri, state)
5159
}

src/main/scala/com/fortysevendeg/github4s/HttpClient.scala

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,15 @@ class HttpClient {
129129
.withData(data)
130130
.run, D)
131131

132+
def postOAuth[A](
133+
url: String,
134+
data: String)
135+
(implicit D: Decoder[A]): GHResponse[A] =
136+
GithubResponses.toEntity(HttpRequestBuilder(url)
137+
.withHeaders(Map("Accept" -> "application/json"))
138+
.withData(data)
139+
.run, D)
140+
132141
def delete(method: String)(implicit C: GithubConfig): GHResponse[Unit] =
133142
GithubResponses.toEmpty(HttpRequestBuilder(buildURL(method))
134143
.deleteMethod

src/main/scala/com/fortysevendeg/github4s/api/Auth.scala

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ object Auth {
1616
protected val httpClient = new HttpClient()
1717

1818
val authorizeUrl = "https://github.com/login/oauth/authorize?client_id=%s&redirect_uri=%s&scope=%s&state=%s"
19+
val accessTokenUrl = "https://github.com/login/oauth/access_token"
1920

2021
/**
2122
* Call to request a new authorization given a basic authentication, the returned object Authorization includes an
@@ -62,13 +63,24 @@ object Auth {
6263
}
6364

6465

66+
/**
67+
* Requests an access token based on the code retrieved in the first step of the oAuth process
68+
* @param client_id
69+
* @param client_secret
70+
* @param code
71+
* @param redirect_uri
72+
* @param state
73+
* @return
74+
*/
6575
def getAccessToken(
6676
client_id: String,
6777
client_secret: String,
6878
code: String,
6979
redirect_uri: String,
70-
state: String): GHResponse[OAuthToken] = ???
71-
)
80+
state: String): GHResponse[OAuthToken] = httpClient.postOAuth[OAuthToken](
81+
url = accessTokenUrl,
82+
data = NewOAuthRequest(client_id, client_secret, code, redirect_uri, state).asJson.noSpaces)
83+
7284

7385

7486
}

src/main/scala/com/fortysevendeg/github4s/free/algebra/AuthOps.scala

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package com.fortysevendeg.github4s.free.algebra
22

33
import cats.free.{Free, Inject}
44
import com.fortysevendeg.github4s.GithubResponses._
5-
import com.fortysevendeg.github4s.free.domain.{Authorize, Authorization}
5+
import com.fortysevendeg.github4s.free.domain.{OAuthToken, Authorize, Authorization}
66

77
/** Auths ops ADT
88
*/
@@ -21,6 +21,13 @@ final case class AuthorizeUrl(
2121
redirect_uri: String,
2222
scopes: List[String]) extends AuthOp[GHResponse[Authorize]]
2323

24+
final case class GetAccessToken(
25+
client_id: String,
26+
client_secret: String,
27+
code: String,
28+
redirect_uri: String,
29+
state: String) extends AuthOp[GHResponse[OAuthToken]]
30+
2431

2532
/** Exposes Auths operations as a Free monadic algebra that may be combined with other Algebras via
2633
* Coproduct
@@ -42,6 +49,14 @@ class AuthOps[F[_]](implicit I: Inject[AuthOp, F]) {
4249
scopes: List[String]): Free[F, GHResponse[Authorize]] =
4350
Free.inject[AuthOp, F](AuthorizeUrl(client_id, redirect_uri, scopes))
4451

52+
def getAccessToken(
53+
client_id: String,
54+
client_secret: String,
55+
code: String,
56+
redirect_uri: String,
57+
state: String): Free[F, GHResponse[OAuthToken]] =
58+
Free.inject[AuthOp, F](GetAccessToken(client_id, client_secret, code, redirect_uri, state))
59+
4560
}
4661

4762
/** Default implicit based DI factory from which instances of the AuthOps may be obtained

src/main/scala/com/fortysevendeg/github4s/free/domain/Authorization.scala

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,11 @@ case class Authorize(
1818
case class OAuthToken(
1919
access_token: String,
2020
token_type: String,
21-
scope: String)
21+
scope: String)
22+
23+
case class NewOAuthRequest(
24+
client_id: String,
25+
client_secret: String,
26+
code: String,
27+
redirect_uri: String,
28+
state: String)

src/main/scala/com/fortysevendeg/github4s/free/interpreters/Interpreters.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ trait Interpreters[M[_]] {
5151
def apply[A](fa: AuthOp[A]): M[A] = fa match {
5252
case NewAuth(username, password, scopes, note, client_id, client_secret) A.pureEval(Eval.later(Auth.newAuth(username, password, scopes, note, client_id, client_secret)))
5353
case AuthorizeUrl(client_id, redirect_uri, scopes) => A.pureEval(Eval.later(Auth.authorizeUrl(client_id, redirect_uri, scopes)))
54+
case GetAccessToken(client_id, client_secret, code, redirect_uri, state) => A.pureEval(Eval.later(Auth.getAccessToken(client_id, client_secret, code, redirect_uri, state)))
5455
}
5556
}
5657

0 commit comments

Comments
 (0)