forked from derabbink/langpop
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented oauth2.0 for stackoverflow
- Loading branch information
Showing
11 changed files
with
289 additions
and
14 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# copy this file to application.conf | ||
# and edit | ||
|
||
langpop-web { | ||
auth { | ||
stackoverflow { | ||
client_id = 123 # your app's id | ||
client_secret = "your own app's super secret client secred" | ||
key = "your app's not so secret key" | ||
redirect_uri = "http://localhost:8080/auth/stackoverflow/redirect" | ||
|
||
credentialsFile = "stackoverflow.auth.properties" # file will be created in execution path of server | ||
} | ||
github { | ||
# nothing yet | ||
} | ||
} | ||
} |
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
57 changes: 57 additions & 0 deletions
57
langpop-web/src/main/scala/com/abbink/langpop/web/AuthServlet.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,57 @@ | ||
package com.abbink.langpop.web | ||
|
||
import org.scalatra.scalate.ScalateSupport | ||
import org.scalatra.ScalatraServlet | ||
import java.net.URLDecoder | ||
|
||
class AuthServlet extends ScalatraServlet with ScalateSupport with ComponentRegistry { | ||
|
||
get("/") { | ||
<html> | ||
<body> | ||
<h1>Login status</h1> | ||
<ul> | ||
<li>StackOverflow: {stackoverflowLogin()}</li> | ||
<li>GitHub: {githubLogin}</li> | ||
<li>Issue query <a href="/langpop">here</a></li> | ||
</ul> | ||
</body> | ||
</html> | ||
} | ||
|
||
private def stackoverflowLogin() = { | ||
if (stackOverflowAuth.isAuthenticated()) { | ||
<strong>signed in</strong> | ||
<a href="/auth/stackoverflow/logout">sign out</a> | ||
} | ||
else { | ||
<strong>signed out</strong> | ||
<a href="/auth/stackoverflow/login">sign in</a> | ||
} | ||
} | ||
|
||
private def githubLogin() = { | ||
<i>not implemented yet</i> | ||
} | ||
|
||
get("/stackoverflow/logout") { | ||
if (stackOverflowAuth.isAuthenticated()) | ||
stackOverflowAuth.clearAuth() | ||
redirect("/auth") | ||
} | ||
|
||
get("/stackoverflow/login") { | ||
if (stackOverflowAuth.isAuthenticated()) | ||
redirect("/auth") | ||
else | ||
redirect(stackOverflowAuth.buildOAuthUrl()) | ||
} | ||
|
||
get("/stackoverflow/redirect") { | ||
val code = URLDecoder.decode(params("code")) | ||
//val state = params.get("state") map URLDecoder.decode | ||
|
||
stackOverflowAuth.finalizeAuth(code) | ||
redirect("/auth") | ||
} | ||
} |
6 changes: 4 additions & 2 deletions
6
langpop-web/src/main/scala/com/abbink/langpop/web/ComponentRegistry.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 |
---|---|---|
@@ -1,9 +1,11 @@ | ||
package com.abbink.langpop.web | ||
|
||
import com.abbink.langpop.aggregate.{ComponentRegistry => AggregatorComponentRegistry} | ||
import com.abbink.langpop.web.auth.StackOverflowAuthComponent | ||
|
||
trait ComponentRegistry extends | ||
AggregatorComponentRegistry | ||
AggregatorComponentRegistry with | ||
StackOverflowAuthComponent | ||
{ | ||
|
||
val stackOverflowAuth = StackOverflowAuthImpl | ||
} |
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
179 changes: 179 additions & 0 deletions
179
langpop-web/src/main/scala/com/abbink/langpop/web/auth/StackOverflowAuth.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,179 @@ | ||
package com.abbink.langpop.web.auth | ||
|
||
import java.io.FileInputStream | ||
import java.io.FileOutputStream | ||
import java.io.InputStream | ||
import java.io.OutputStream | ||
import java.util.ArrayList | ||
import java.util.Date | ||
import java.util.Properties | ||
import org.apache.http.client.entity.UrlEncodedFormEntity | ||
import org.apache.http.client.methods.HttpPost | ||
import org.apache.http.client.utils.URIBuilder | ||
import org.apache.http.client.HttpClient | ||
import org.apache.http.impl.client.DefaultHttpClient | ||
import org.apache.http.message.BasicNameValuePair | ||
import org.apache.http.HttpResponse | ||
import org.apache.http.NameValuePair | ||
import com.typesafe.config.ConfigFactory | ||
import org.apache.http.util.EntityUtils | ||
import org.apache.http.client.utils.URLEncodedUtils | ||
import java.net.URI | ||
import java.nio.charset.Charset | ||
|
||
trait StackOverflowAuth { | ||
|
||
def isAuthenticated() : Boolean | ||
|
||
def buildOAuthUrl() : String | ||
|
||
def clearAuth() : Unit | ||
|
||
def finalizeAuth(code : String) : Unit | ||
} | ||
|
||
trait StackOverflowAuthComponent { | ||
val stackOverflowAuth:StackOverflowAuth | ||
|
||
object StackOverflowAuthImpl extends StackOverflowAuth { | ||
|
||
val config = ConfigFactory.load() | ||
val mergedConfig = config.getConfig("langpop-web").withFallback(config) | ||
|
||
val client_id = mergedConfig.getString("langpop-web.auth.stackoverflow.client_id") | ||
val client_secret = mergedConfig.getString("langpop-web.auth.stackoverflow.client_secret") | ||
val scope = "no_expiry" | ||
val redirect_uri = mergedConfig.getString("langpop-web.auth.stackoverflow.redirect_uri") | ||
val credentialsFileName = mergedConfig.getString("langpop-web.auth.stackoverflow.credentialsFile") | ||
|
||
var access_token : Option[String] = None | ||
var expires : Option[Date] = None | ||
readAuth() | ||
|
||
def isAuthenticated() = { | ||
access_token match { | ||
case None => false | ||
case Some(t) => expires match { | ||
case None => true | ||
case Some(e) => e after new Date() | ||
} | ||
} | ||
} | ||
|
||
def buildOAuthUrl() = { | ||
val uriBuilder = new URIBuilder(); | ||
uriBuilder.setScheme("https").setHost("stackexchange.com").setPath("/oauth") | ||
.setParameter("client_id", client_id) | ||
.setParameter("scope", scope) | ||
.setParameter("redirect_uri", redirect_uri) | ||
uriBuilder.build.toString() | ||
} | ||
|
||
/** | ||
* reads access token and expiration date from properties file | ||
*/ | ||
private def readAuth() : Unit = { | ||
access_token = None | ||
expires = None | ||
try { | ||
val fs : InputStream = new FileInputStream(credentialsFileName); | ||
val props : Properties = new Properties() | ||
props.load(fs); | ||
fs.close() | ||
|
||
val token = props.getProperty("access_token") | ||
val exp = props.getProperty("expires") | ||
access_token = token match { | ||
case null => None | ||
case x => Some(x) | ||
} | ||
expires = exp match {case null => None case x => Some(new Date(1000 * (x.toLong)))} | ||
} | ||
catch { | ||
case e => //TODO | ||
} | ||
(access_token, expires) | ||
} | ||
|
||
/** | ||
* clears all auth data (i.e. signs out) | ||
*/ | ||
def clearAuth() = { | ||
access_token = None | ||
expires = None | ||
try { | ||
val props = new Properties() | ||
val fs : OutputStream = new FileOutputStream(credentialsFileName) | ||
props.store(fs, null) | ||
fs.close() | ||
} | ||
catch { | ||
case e => //TODO | ||
} | ||
} | ||
|
||
def finalizeAuth(code : String) = { | ||
val uriBuilder = new URIBuilder() | ||
uriBuilder.setScheme("https").setHost("stackexchange.com").setPath("/oauth/access_token") | ||
val client : HttpClient = new DefaultHttpClient() | ||
val formparams : java.util.List[NameValuePair] = new ArrayList[NameValuePair]() | ||
formparams.add(new BasicNameValuePair("client_id", client_id)) | ||
formparams.add(new BasicNameValuePair("client_secret", client_secret)) | ||
formparams.add(new BasicNameValuePair("code", code)) | ||
formparams.add(new BasicNameValuePair("redirect_uri", redirect_uri)) | ||
val entity : UrlEncodedFormEntity = new UrlEncodedFormEntity(formparams, "UTF-8") | ||
val post = new HttpPost(uriBuilder.build()) | ||
post.setEntity(entity) | ||
val response : HttpResponse = client.execute(post) | ||
|
||
if (response.getStatusLine().getStatusCode() != 400) { | ||
var access_token : Option[String] = None | ||
var expires : Option[Date] = None | ||
|
||
val entity = response.getEntity() | ||
val entityContent = EntityUtils.toString(entity) | ||
val entities : java.util.List[NameValuePair] = URLEncodedUtils.parse( | ||
entityContent, | ||
Charset.forName(entity.getContentEncoding() match { | ||
case null => "ISO-8859-1" //default from EntityUtils | ||
case x => x.getValue() | ||
})) | ||
val iter = entities.iterator() | ||
while (iter.hasNext()) { | ||
val pair : NameValuePair = iter.next() | ||
pair.getName() match { | ||
case "access_token" => access_token = Some(pair.getValue()) | ||
case "expires" => expires = Some(new Date(1000 * (pair.getValue().toLong))) | ||
case _ => //ignore | ||
} | ||
} | ||
|
||
writeAuth(access_token, expires) | ||
} | ||
} | ||
|
||
private def writeAuth(accessToken : Option[String], expires : Option[Date]) = { | ||
if (accessToken == None) { | ||
clearAuth | ||
} | ||
else { | ||
this.access_token = accessToken | ||
this.expires = expires | ||
try { | ||
val props = new Properties() | ||
props.setProperty("access_token", accessToken.get); | ||
expires match { | ||
case Some(date) => props.setProperty("expires", (date.getTime()/1000).toString()) | ||
case _ => //ignore | ||
} | ||
val fs : OutputStream = new FileOutputStream(credentialsFileName); | ||
props.store(fs, null); | ||
fs.close() | ||
} | ||
catch { | ||
case e => //TODO | ||
} | ||
} | ||
} | ||
} | ||
} |
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