-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from zamblauskas/basic-auth-merge
Basic auth support
- Loading branch information
Showing
9 changed files
with
96 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/main/scala/com/codacy/client/bitbucket/client/Authentication.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package com.codacy.client.bitbucket.client | ||
|
||
import play.api.libs.oauth.{ConsumerKey, OAuthCalculator, RequestToken} | ||
import play.api.libs.ws.{WSAuthScheme, WSRequest} | ||
|
||
/** | ||
* Handles request authentication. | ||
* Provides several different authentication options. | ||
* | ||
* @author - Robertas Zamblauskas | ||
*/ | ||
object Authentication { | ||
|
||
sealed trait Credentials | ||
|
||
case class OAuthCredentials(key: String, secretKey: String, token: String, secretToken: String) extends Credentials | ||
|
||
/** | ||
* Your username and password | app password. | ||
*/ | ||
case class BasicAuthCredentials(username: String, password: String) extends Credentials | ||
|
||
|
||
sealed trait Authenticator { | ||
def authenticate(req: WSRequest): WSRequest | ||
} | ||
|
||
object Authenticator { | ||
def fromCredentials(credentials: Credentials): Authenticator = { | ||
credentials match { | ||
case c: OAuthCredentials => new OAuthAuthenticator(c) | ||
case c: BasicAuthCredentials => new BasicAuthAuthenticator(c) | ||
} | ||
} | ||
} | ||
|
||
class OAuthAuthenticator(credentials: OAuthCredentials) extends Authenticator { | ||
private lazy val KEY = ConsumerKey(credentials.key, credentials.secretKey) | ||
private lazy val TOKEN = RequestToken(credentials.token, credentials.secretToken) | ||
|
||
private lazy val requestSigner = OAuthCalculator(KEY, TOKEN) | ||
|
||
def authenticate(req: WSRequest): WSRequest = req.sign(requestSigner) | ||
} | ||
|
||
class BasicAuthAuthenticator(credentials: BasicAuthCredentials) extends Authenticator { | ||
def authenticate(req: WSRequest): WSRequest = req.withAuth(credentials.username, credentials.password, WSAuthScheme.BASIC) | ||
} | ||
|
||
/** | ||
* Provide nicer syntax for authentication. | ||
*/ | ||
implicit class WsRequestExtensions(val req: WSRequest) extends AnyVal { | ||
def authenticate(authenticator: Authenticator): WSRequest = authenticator.authenticate(req) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters