This repository has been archived by the owner on Feb 17, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 730
Add IR/Magfa sms gateway #1021
Open
mah454
wants to merge
4
commits into
actorapp:master
Choose a base branch
from
mah454:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add IR/Magfa sms gateway #1021
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
...rver/actor-activation/src/main/scala/im/actor/server/activation/magfa/MagfaProvider.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,60 @@ | ||
package im.actor.server.activation.magfa | ||
|
||
import akka.actor.ActorSystem | ||
import akka.pattern.ask | ||
import akka.util.Timeout | ||
import cats.data.Xor | ||
import im.actor.config.ActorConfig | ||
import im.actor.server.activation.common.ActivationStateActor.{ ForgetSentCode, Send, SendAck } | ||
import im.actor.server.activation.common._ | ||
import im.actor.server.db.DbExtension | ||
import im.actor.server.model.AuthPhoneTransaction | ||
import im.actor.server.persist.auth.AuthTransactionRepo | ||
import im.actor.server.sms._ | ||
import im.actor.util.misc.PhoneNumberUtils.isTestPhone | ||
|
||
import scala.concurrent.Future | ||
import scala.concurrent.duration._ | ||
|
||
private[activation] final class MagfaProvider(implicit system: ActorSystem) extends ActivationProvider with CommonAuthCodes { | ||
|
||
protected val activationConfig = ActivationConfig.load.getOrElse(throw new RuntimeException("Failed to load activation config")) | ||
protected val db = DbExtension(system).db | ||
protected implicit val ec = system.dispatcher | ||
|
||
private val telesignClient = new MagfaClient(ActorConfig.load().getConfig("services.magfa")) | ||
private val smsEngine = new MagfaSmsEngine(telesignClient) | ||
|
||
private implicit val timeout = Timeout(20.seconds) | ||
|
||
private val smsStateActor = system.actorOf(ActivationStateActor.props[Long, SmsCode]( | ||
repeatLimit = activationConfig.repeatLimit, | ||
sendAction = (code: SmsCode) ⇒ smsEngine.sendCode(code.phone, code.code), | ||
id = (code: SmsCode) ⇒ code.phone | ||
), "telesign-sms-state") | ||
|
||
override def send(txHash: String, code: Code): Future[CodeFailure Xor Unit] = code match { | ||
case s: SmsCode ⇒ | ||
for { | ||
resp ← if (isTestPhone(s.phone)) | ||
Future.successful(Xor.right(())) | ||
else | ||
(smsStateActor ? Send(code)).mapTo[SendAck].map(_.result) | ||
_ ← createAuthCodeIfNeeded(resp, txHash, code.code) | ||
} yield resp | ||
case other ⇒ throw new RuntimeException(s"This provider can't handle code of type: ${other.getClass}") | ||
} | ||
|
||
override def cleanup(txHash: String): Future[Unit] = { | ||
for { | ||
ac ← db.run(AuthTransactionRepo.findChildren(txHash)) | ||
_ = ac match { | ||
case Some(x: AuthPhoneTransaction) ⇒ | ||
smsStateActor ! ForgetSentCode.phone(x.phoneNumber) | ||
case _ ⇒ | ||
} | ||
_ ← deleteAuthCode(txHash) | ||
} yield () | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
actor-server/actor-sms/src/main/scala/im/actor/server/sms/MagfaClient.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,50 @@ | ||
package im.actor.server.sms | ||
|
||
import java.net.URLEncoder | ||
|
||
import akka.actor.ActorSystem | ||
import com.ning.http.client.Response | ||
import com.typesafe.config.Config | ||
import dispatch.{ Http, url } | ||
|
||
import scala.concurrent.{ ExecutionContext, Future } | ||
import scala.util.Failure | ||
|
||
final class MagfaClient(config: Config)(implicit system: ActorSystem) { | ||
|
||
private lazy val http = new Http() | ||
private val Utf8Encoding = "UTF-8" | ||
system registerOnTermination http.shutdown() | ||
|
||
private implicit val ec: ExecutionContext = system.dispatcher | ||
|
||
private val BaseUrl = config.getString("url") | ||
// private val ResourcePath = config.getString("/services/send") | ||
private val ResourcePath = "/services/send?" | ||
|
||
def sendSmsCode(phoneNumber: Long, code: String): Future[Unit] = { | ||
postRequest(ResourcePath, Map( | ||
"from" → config.getString("from"), | ||
"to" → phoneNumber.toString, | ||
"message" → code | ||
)) map { _ ⇒ | ||
system.log.debug("Message sent via magfa") | ||
} | ||
} | ||
|
||
private def postRequest(resourcePath: String, params: Map[String, String]): Future[Response] = { | ||
val body = params.map(p ⇒ s"${p._1}=${URLEncoder.encode(p._2, Utf8Encoding)}").mkString("&") | ||
val request = url(BaseUrl + resourcePath + body) | ||
|
||
http(request).map { resp ⇒ | ||
if (resp.getStatusCode < 199 || resp.getStatusCode > 299) { | ||
throw new RuntimeException(s"Response has code ${resp.getStatusCode}. [${resp.getResponseBody}]") | ||
} else { | ||
resp | ||
} | ||
} andThen { | ||
case Failure(e) ⇒ | ||
system.log.error(e, "Failed to make request to telesign") | ||
} | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
actor-server/actor-sms/src/main/scala/im/actor/server/sms/MagfaSmsEngine.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,7 @@ | ||
package im.actor.server.sms | ||
|
||
import scala.concurrent.Future | ||
|
||
final class MagfaSmsEngine(magfaSmsClient: MagfaClient) extends AuthSmsEngine { | ||
override def sendCode(phoneNumber: Long, message: String): Future[Unit] = magfaSmsClient.sendSmsCode(phoneNumber, message) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
you forgot to change variable name