<commit_before>/**
 * Copyright 2014 <NAME>
 * 
 * Based on the code for LinkedInOAuth2Provider by <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package securesocial.core.providers

import securesocial.core._
import play.api.libs.oauth.{RequestToken, OAuthCalculator}
import play.api.libs.ws.WS
import play.api.{Application, Logger}
import DropboxProvider._

/**
 * A Dropbox Provider (OAuth2)
 */
class DropboxProvider(application: Application) extends OAuth2Provider(application) {

  override def id = DropboxProvider.Dropbox

  override def fillProfile(user: SocialUser): SocialUser = {
    val accessToken = user.oAuth2Info.get.accessToken
    val promise = WS.url(DropboxProvider.Api).
      withHeaders("Authorization"->s"Bearer $accessToken").get()

     try {
        val response = awaitResult(promise)
        response.status match {
          case 200 => 
            val data = response.json
            val userId = (data \ Id).as[Int]
            val fullName = (data \ FormattedName).as[String]
            //Derive guessed first and last names from the full name, which is all that Dropbox provides
            val index=fullName.indexOf(" ")
            val (firstName,lastName)=
              if (index>=0) (fullName.substring(0,index),fullName.substring(index))
              else (fullName,"")
            SocialUser(user).copy(
              identityId = IdentityId(userId.toString, id),
              firstName=firstName,
              lastName=lastName,
              fullName= fullName
            )
          case _ =>
            Logger.error("[securesocial] Dropbox account info request returned error: "+response.body)
            throw new AuthenticationException()
        }
     } catch {
       case e: Exception => {
         Logger.error("[securesocial] error retrieving profile information from Dropbox", e)
         throw new AuthenticationException()
       }
     }
  }
}

object DropboxProvider {
  val Api = "https://api.dropbox.com/1/account/info"
  val Dropbox = "dropbox"
  val Id = "uid"
  val FormattedName = "display_name"<commit_msg>Updated logging and no longer autocompute first and last names from full name<commit_after>/**
 * Copyright 2014 <NAME>
 * 
 * Based on the code for LinkedInOAuth2Provider by <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */
package securesocial.core.providers

import securesocial.core._
import play.api.libs.oauth.{RequestToken, OAuthCalculator}
import play.api.libs.ws.WS
import play.api.Application
import DropboxProvider._

/**
 * A Dropbox Provider (OAuth2)
 */
class DropboxProvider(application: Application) extends OAuth2Provider(application) {
  private val Logger=play.api.Logger("securesocial.core.providers.DropboxProvider")
  override def id = DropboxProvider.Dropbox

  override def fillProfile(user: SocialUser): SocialUser = {
    val accessToken = user.oAuth2Info.get.accessToken
    val promise = WS.url(DropboxProvider.Api).
      withHeaders("Authorization"->s"Bearer $accessToken").get()

     try {
        val response = awaitResult(promise)
        response.status match {
          case 200 => 
            val data = response.json
            val userId = (data \ Id).as[Int]
            val fullName = (data \ FormattedName).as[String]
            SocialUser(user).copy(
              identityId = IdentityId(userId.toString, id),
              fullName= fullName
            )
          case _ =>
            Logger.error("[securesocial] Dropbox account info request returned error: "+response.body)
            throw new AuthenticationException()
        }
     } catch {
       case e: Exception => {
         Logger.error("[securesocial] error retrieving profile information from Dropbox", e)
         throw new AuthenticationException()
       }
     }
  }
}

object DropboxProvider {
  val Api = "https://api.dropbox.com/1/account/info"
  val Dropbox = "dropbox"
  val Id = "uid"
  val FormattedName = "display_name"
{'fraction_non_alphanumeric': 0.09258887326772444, 'fraction_numerical': 0.006828680457923278, 'mean_word_length': 2.8845553822152885, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 16, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '937532', 'n_tokens_mistral': 1363, 'n_tokens_neox': 1333, 'n_words': 432}