├── project ├── build.properties ├── plugins.sbt ├── LibDependencies.scala └── ScoverageSettings.scala ├── repository.yaml ├── .gitignore ├── shared └── src │ ├── main │ └── scala │ │ └── uk │ │ └── gov │ │ └── hmrc │ │ └── emailaddress │ │ ├── StringValue.scala │ │ ├── PlayJsonFormats.scala │ │ ├── ObfuscatedEmailAddress.scala │ │ └── EmailAddress.scala │ └── test │ └── scala │ └── uk │ └── gov │ └── hmrc │ └── emailaddress │ ├── PlayJsonFormatsSpec.scala │ ├── EmailAddressGenerators.scala │ ├── ObfuscateedEmailAddressSpec.scala │ └── EmailAddressSpec.scala ├── README.md ├── scalastyle-config.xml └── LICENSE /project/build.properties: -------------------------------------------------------------------------------- 1 | sbt.version=1.9.7 2 | -------------------------------------------------------------------------------- /repository.yaml: -------------------------------------------------------------------------------- 1 | repoVisibility: public_0C3F0CE3E6E6448FAD341E7BFA50FCD333E06A20CFF05FCACE61154DDBBADF71 2 | description: Micro-library for validating and obfuscating email addresses 3 | deprecated: true 4 | -------------------------------------------------------------------------------- /project/plugins.sbt: -------------------------------------------------------------------------------- 1 | resolvers += MavenRepository("HMRC-open-artefacts-maven2", "https://open.artefacts.tax.service.gov.uk/maven2") 2 | resolvers += Resolver.url("HMRC-open-artefacts-ivy2", url("https://open.artefacts.tax.service.gov.uk/ivy2"))(Resolver.ivyStylePatterns) 3 | 4 | addSbtPlugin("uk.gov.hmrc" % "sbt-auto-build" % "3.15.0") 5 | addSbtPlugin("org.scoverage" % "sbt-scoverage" % "2.0.9") 6 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.class 2 | target 3 | logs 4 | lib_managed 5 | pids 6 | 7 | */*dependency-reduced-pom.xml 8 | 9 | *.jar 10 | *.war 11 | *.ear 12 | 13 | *.ipr 14 | *.iws 15 | *.iml 16 | **/*.iml 17 | .idea 18 | 19 | *.DS_Store 20 | **/*.DS_Store 21 | 22 | **/target/* 23 | target 24 | 25 | **/bin/* 26 | **/lib/* 27 | *.classpath 28 | *.project 29 | **/*.settings 30 | .project 31 | .settings 32 | .classpath 33 | .sass-cache 34 | 35 | nohup.out 36 | rebel.xml 37 | .history 38 | .bsp -------------------------------------------------------------------------------- /project/LibDependencies.scala: -------------------------------------------------------------------------------- 1 | import sbt.* 2 | 3 | object LibDependencies { 4 | 5 | sealed trait PlayVersion 6 | 7 | object PlayVersion { 8 | 9 | case object Play28 extends PlayVersion 10 | 11 | case object Play29 extends PlayVersion 12 | 13 | case object Play30 extends PlayVersion 14 | 15 | } 16 | 17 | def compileDependencies(playVersion: PlayVersion) = playVersion match { 18 | case PlayVersion.Play29 => "com.typesafe.play" %% "play" % "2.9.0" 19 | case PlayVersion.Play30 => "org.playframework" %% "play" % "3.0.0" 20 | } 21 | 22 | val testDependencies = Seq( 23 | "org.scalatest" %% "scalatest" % "3.2.17", 24 | "org.scalatestplus" %% "scalacheck-1-17" % "3.2.14.0", 25 | "org.pegdown" % "pegdown" % "1.6.0", 26 | "org.scalacheck" %% "scalacheck" % "1.17.0", 27 | "com.vladsch.flexmark" % "flexmark-all" % "0.64.8" 28 | ).map(_ % Test) 29 | 30 | } 31 | -------------------------------------------------------------------------------- /shared/src/main/scala/uk/gov/hmrc/emailaddress/StringValue.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 HM Revenue & Customs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.gov.hmrc.emailaddress 18 | 19 | object StringValue { 20 | implicit def stringValueToString(e: StringValue): String = e.value 21 | } 22 | 23 | trait StringValue { 24 | def value: String 25 | override def toString: String = value 26 | } 27 | -------------------------------------------------------------------------------- /project/ScoverageSettings.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2021 HM Revenue & Customs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | import sbt.Keys.parallelExecution 18 | import sbt._ 19 | import scoverage.ScoverageKeys 20 | 21 | object ScoverageSettings { 22 | def apply(): Seq[Def.Setting[_ >: String with Double with Boolean]] = 23 | Seq( 24 | // Semicolon-separated list of regexes matching classes to exclude 25 | ScoverageKeys.coverageExcludedPackages := ";.*Reverse.*;.*(config|testonly).*;.*(BuildInfo|Routes).*", 26 | ScoverageKeys.coverageMinimumStmtTotal := 97.00, 27 | ScoverageKeys.coverageFailOnMinimum := true, 28 | ScoverageKeys.coverageHighlighting := true, 29 | Test / parallelExecution := false 30 | ) 31 | } 32 | -------------------------------------------------------------------------------- /shared/src/main/scala/uk/gov/hmrc/emailaddress/PlayJsonFormats.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 HM Revenue & Customs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.gov.hmrc.emailaddress 18 | 19 | object PlayJsonFormats { 20 | import play.api.libs.json._ 21 | 22 | implicit val emailAddressReads: Reads[EmailAddress] = new Reads[EmailAddress] { 23 | def reads(js: JsValue): JsResult[EmailAddress] = js.validate[String].flatMap { 24 | case s if EmailAddress.isValid(s) => JsSuccess(EmailAddress(s)) 25 | case _ => JsError("not a valid email address") 26 | } 27 | } 28 | implicit val emailAddressWrites: Writes[EmailAddress] = new Writes[EmailAddress] { 29 | def writes(e: EmailAddress): JsValue = JsString(e.value) 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /shared/src/test/scala/uk/gov/hmrc/emailaddress/PlayJsonFormatsSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 HM Revenue & Customs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.gov.hmrc.emailaddress 18 | 19 | import org.scalatest.matchers.should.Matchers 20 | import org.scalatest.wordspec.AnyWordSpec 21 | import play.api.libs.json.{JsError, JsString, JsSuccess, Json} 22 | 23 | class PlayJsonFormatsSpec extends AnyWordSpec with Matchers { 24 | 25 | import PlayJsonFormats._ 26 | 27 | "Reading an EmailAddress from JSON" should { 28 | 29 | "work for a valid email address" in { 30 | val result = JsString("a@b.com").validate[EmailAddress] 31 | result shouldBe a [JsSuccess[_]] 32 | result.get should be (EmailAddress("a@b.com")) 33 | } 34 | 35 | "fail for a invalid email address" in { 36 | val result = JsString("ab.com").validate[EmailAddress] 37 | result shouldBe a [JsError] 38 | } 39 | } 40 | 41 | "Writing an EmailAddress to JSON" should { 42 | 43 | "work!" in { 44 | Json.toJson(EmailAddress("a@b.com")) should be (JsString("a@b.com")) 45 | } 46 | } 47 | } 48 | -------------------------------------------------------------------------------- /shared/src/test/scala/uk/gov/hmrc/emailaddress/EmailAddressGenerators.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 HM Revenue & Customs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.gov.hmrc.emailaddress 18 | 19 | import org.scalacheck.Gen 20 | import org.scalacheck.Gen._ 21 | 22 | trait EmailAddressGenerators { 23 | 24 | def nonEmptyString(char: Gen[Char]): Gen[String] = 25 | nonEmptyListOf(char) 26 | .map(_.mkString) 27 | .suchThat(_.nonEmpty) 28 | 29 | def chars(chars: String): Gen[Char] = Gen.choose(0, chars.length - 1).map(chars.charAt) 30 | 31 | val validMailbox: Gen[String] = nonEmptyString(oneOf(alphaChar, chars(".!#$%&’'*+/=?^_`{|}~-"))).label("mailbox") 32 | 33 | val validDomain: Gen[String] = (for { 34 | topLevelDomain <- nonEmptyString(alphaChar) 35 | otherParts <- listOf(nonEmptyString(alphaChar)) 36 | } yield (otherParts :+ topLevelDomain).mkString(".")).label("domain") 37 | 38 | def validEmailAddresses(mailbox: Gen[String] = validMailbox, domain: Gen[String] = validDomain): Gen[String] = 39 | for { 40 | mailbox <- mailbox 41 | domain <- domain 42 | } yield s"$mailbox@$domain" 43 | } 44 | -------------------------------------------------------------------------------- /shared/src/main/scala/uk/gov/hmrc/emailaddress/ObfuscatedEmailAddress.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 HM Revenue & Customs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.gov.hmrc.emailaddress 18 | 19 | import scala.util.matching.Regex 20 | 21 | trait ObfuscatedEmailAddress { 22 | val value: String 23 | override def toString: String = value 24 | } 25 | 26 | object ObfuscatedEmailAddress { 27 | final private val shortMailbox: Regex = "(.{1,2})".r 28 | final private val longMailbox: Regex = "(.)(.*)(.)".r 29 | 30 | import EmailAddress.validEmail 31 | 32 | implicit def obfuscatedEmailToString(e: ObfuscatedEmailAddress): String = e.value 33 | 34 | def apply(plainEmailAddress: String): ObfuscatedEmailAddress = new ObfuscatedEmailAddress { 35 | val value: String = plainEmailAddress match { 36 | case validEmail(shortMailbox(m), domain) => 37 | s"${obscure(m)}@$domain" 38 | 39 | case validEmail(longMailbox(firstLetter,middle,lastLetter), domain) => 40 | s"$firstLetter${obscure(middle)}$lastLetter@$domain" 41 | 42 | case invalidEmail => 43 | throw new IllegalArgumentException(s"Cannot obfuscate invalid email address '$invalidEmail'") 44 | } 45 | } 46 | 47 | private def obscure(text: String): String = "*" * text.length 48 | } 49 | -------------------------------------------------------------------------------- /shared/src/main/scala/uk/gov/hmrc/emailaddress/EmailAddress.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 HM Revenue & Customs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.gov.hmrc.emailaddress 18 | 19 | case class EmailAddress(value: String) extends StringValue { 20 | 21 | val (mailbox, domain): (EmailAddress.Mailbox, EmailAddress.Domain) = value match { 22 | case EmailAddress.validEmail(m, d) => (EmailAddress.Mailbox(m), EmailAddress.Domain(d)) 23 | case invalidEmail => throw new IllegalArgumentException(s"'$invalidEmail' is not a valid email address") 24 | } 25 | 26 | lazy val obfuscated: ObfuscatedEmailAddress = ObfuscatedEmailAddress.apply(value) 27 | } 28 | 29 | object EmailAddress { 30 | final private[emailaddress] val validDomain = """^([a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*)$""".r 31 | final private[emailaddress] val validEmail = """^([a-zA-Z0-9.!#$%&’'*+/=?^_`{|}~-]+)@([a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*)$""".r 32 | 33 | def isValid(email: String): Boolean = email match { 34 | case validEmail(_,_) => true 35 | case _ => false 36 | } 37 | 38 | case class Mailbox private[EmailAddress] (value: String) extends StringValue 39 | case class Domain(value: String) extends StringValue { 40 | value match { 41 | case EmailAddress.validDomain(_) => // 42 | case invalidDomain => throw new IllegalArgumentException(s"'$invalidDomain' is not a valid email domain") 43 | } 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /shared/src/test/scala/uk/gov/hmrc/emailaddress/ObfuscateedEmailAddressSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 HM Revenue & Customs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.gov.hmrc.emailaddress 18 | 19 | import org.scalatest.matchers.should.Matchers 20 | import org.scalatest.wordspec.AnyWordSpec 21 | import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks 22 | 23 | class ObfuscateedEmailAddressSpec extends AnyWordSpec with Matchers with ScalaCheckPropertyChecks with EmailAddressGenerators { 24 | 25 | "Obfuscating an email address" should { 26 | "work for a valid email address with a long mailbox" in { 27 | ObfuscatedEmailAddress("abcdef@example.com").value should be("a****f@example.com") 28 | } 29 | 30 | "work for a valid email address with a single letter mailbox" in { 31 | ObfuscatedEmailAddress("a@example.com").value should be("*@example.com") 32 | } 33 | 34 | "work for a valid email address with a two letter mailbox" in { 35 | ObfuscatedEmailAddress("ab@example.com").value should be("**@example.com") 36 | } 37 | 38 | "work for a valid email address with a three letter mailbox" in { 39 | ObfuscatedEmailAddress("abc@example.com").value should be("a*c@example.com") 40 | } 41 | 42 | "do nothing for a valid email address with a three letter mailbox with * in the middle" in { 43 | ObfuscatedEmailAddress("a*c@example.com").value should be("a*c@example.com") 44 | } 45 | 46 | "work for valid email addresses with a mailbox longer than three chars" in { 47 | forAll (validEmailAddresses(mailbox = validMailbox.suchThat(_.length > 3))) { address => 48 | EmailAddress(address).obfuscated.value should ((not be address) and include("*")) 49 | } 50 | } 51 | 52 | "generate an exception for an invalid email address" in { 53 | an[IllegalArgumentException] should be thrownBy { ObfuscatedEmailAddress("sausages") } 54 | } 55 | 56 | "generate an exception for empty" in { 57 | an[IllegalArgumentException] should be thrownBy { ObfuscatedEmailAddress("") } 58 | } 59 | } 60 | "An ObfuscatedEmailAddress class" should { 61 | "implicitly convert to an obfuscated String of the address" in { 62 | val e: String = ObfuscatedEmailAddress("test@domain.com") 63 | e should be ("t**t@domain.com") 64 | } 65 | "toString to an obfuscated String of the address" in { 66 | val e = ObfuscatedEmailAddress("test@domain.com") 67 | e.toString should be ("t**t@domain.com") 68 | } 69 | } 70 | } 71 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | ## emailaddress 2 | 3 | This library is Deprecated. 4 | You can reach out Digital-Contact team for further assistance.
5 | Alternatively you can write you own EmailAddress class as in the following example: https://github.com/hmrc/preferences-frontend/blob/main/app/emailaddress/EmailAddress.scala 6 | 7 | 8 | [![Join the chat at https://gitter.im/hmrc/emailaddress](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/hmrc/emailaddress?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Build Status](https://travis-ci.org/hmrc/emailaddress.svg?branch=master)](https://travis-ci.org/hmrc/emailaddress) [ ![Download](https://api.bintray.com/packages/hmrc/releases/emailaddress/images/download.svg) ](https://bintray.com/hmrc/releases/emailaddress/_latestVersion) 9 | 10 | Scala micro-library for typing, validating and obfuscating email addresses 11 | 12 | ### Address Typing & Validation 13 | The `EmailAddress` class will only accept valid addresses: 14 | 15 | ```scala 16 | scala> import uk.gov.hmrc.emailaddress._ 17 | import uk.gov.hmrc.emailaddress._ 18 | 19 | scala> EmailAddress("example@test.com") 20 | res0: uk.gov.hmrc.emailaddress.EmailAddress = example@test.com 21 | 22 | scala> EmailAddress("not_a_meaningful_address") 23 | java.lang.IllegalArgumentException: requirement failed: 'not_a_meaningful_address' is not a valid email address 24 | ``` 25 | 26 | You can also use `EmailAddress.isValid(...)`: 27 | 28 | ```scala 29 | scala> EmailAddress.isValid("example@test.com") 30 | res2: Boolean = true 31 | 32 | scala> EmailAddress.isValid("not_a_meaningful_address") 33 | res3: Boolean = false 34 | ``` 35 | 36 | ### Accessing the domain and mailbox 37 | 38 | You can access the mailbox and domain of a given address: 39 | 40 | ```scala 41 | scala> EmailAddress("example@test.com").domain 42 | res0: uk.gov.hmrc.emailaddress.EmailAddress.Domain = test.com 43 | 44 | scala> EmailAddress("example@test.com").mailbox 45 | res1: uk.gov.hmrc.emailaddress.EmailAddress.Mailbox = example 46 | ``` 47 | 48 | These compare equal as you might expect: 49 | 50 | ```scala 51 | scala> EmailAddress("example@test.com").domain == EmailAddress("another@test.com").domain 52 | res2: Boolean = true 53 | 54 | scala> EmailAddress("example@test.com").domain == EmailAddress("another@test.co.uk").domain 55 | res3: Boolean = false 56 | ``` 57 | 58 | ### Obfuscation 59 | Addresses are obfuscated by starring out all of their mailbox part, apart from the first and last letters: 60 | 61 | ```scala 62 | scala> ObfuscatedEmailAddress("example@test.com") 63 | res4: uk.gov.hmrc.emailaddress.ObfuscatedEmailAddress = e*****e@test.com 64 | ``` 65 | Unless there are only two letters: 66 | 67 | ```scala 68 | scala> ObfuscatedEmailAddress("ex@test.com") 69 | res7: uk.gov.hmrc.emailaddress.ObfuscatedEmailAddress = **@test.com``` 70 | 71 | ``` 72 | 73 | You can also create them directly from an `EmailAddress`: 74 | 75 | ```scala 76 | scala> EmailAddress("example@test.com").obfuscated 77 | res6: uk.gov.hmrc.emailaddress.ObfuscatedEmailAddress = e*****e@test.com 78 | ``` 79 | 80 | 81 | ### Converting back to `String` 82 | All classes `toString` and implicitly convert to `String`s nicely: 83 | 84 | ```scala 85 | scala> val someString: String = EmailAddress("example@test.com") 86 | someString: String = example@test.com 87 | 88 | scala> val someString = EmailAddress("example@test.com").toString 89 | someString: String = example@test.com 90 | 91 | scala> val someString: String = ObfuscatedEmailAddress("example@test.com") 92 | someString: String = e*****e@test.com 93 | 94 | scala> val someString = ObfuscatedEmailAddress("example@test.com").toString 95 | someString: String = e*****e@test.com 96 | 97 | scala> EmailAddress("example@test.com").domain.toString 98 | res4: String = test.com 99 | 100 | scala> val s: String = EmailAddress("example@test.com").domain 101 | s: String = test.com 102 | 103 | scala> EmailAddress("example@test.com").mailbox.toString 104 | res5: String = example 105 | 106 | scala> val s: String = EmailAddress("example@test.com").mailbox 107 | s: String = example 108 | ``` 109 | 110 | ### Installing 111 | 112 | Include the following dependency in your SBT build **before v4.0.0** 113 | 114 | ```scala 115 | resolvers += Resolver.bintrayRepo("hmrc", "releases") 116 | 117 | libraryDependencies += "uk.gov.hmrc" %% "emailaddress" % "" 118 | ``` 119 | 120 | Include one the following dependencies in your SBT build for **v4.0.0 or after** depending on whether you are using Play 2.8, 121 | Play 2.9 or Play 3.0 122 | ```scala 123 | libraryDependencies += "uk.gov.hmrc" %% "emailaddress-play-28" % "" 124 | 125 | OR 126 | 127 | libraryDependencies += "uk.gov.hmrc" %% "emailaddress-play-29" % "" 128 | 129 | OR 130 | 131 | libraryDependencies += "uk.gov.hmrc" %% "emailaddress-play-30" % "" 132 | ``` 133 | 134 | ## Run the tests and sbt fmt before raising a PR 135 | 136 | Format: 137 | 138 | `sbt fmt` 139 | 140 | Then run the tests and coverage report: 141 | 142 | `sbt clean coverage test coverageReport` 143 | 144 | If your build fails due to poor test coverage, *DO NOT* lower the test coverage threshold, instead inspect the generated report located here on your local repo: `/target/scala-2.12/scoverage-report/index.html` 145 | 146 | Then run the integration tests: 147 | 148 | `sbt it:test` 149 | 150 | ## License ## 151 | 152 | This code is open source software licensed under the [Apache 2.0 License]("http://www.apache.org/licenses/LICENSE-2.0.html"). 153 | -------------------------------------------------------------------------------- /scalastyle-config.xml: -------------------------------------------------------------------------------- 1 | 2 | Scalastyle standard configuration 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | -------------------------------------------------------------------------------- /shared/src/test/scala/uk/gov/hmrc/emailaddress/EmailAddressSpec.scala: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright 2022 HM Revenue & Customs 3 | * 4 | * Licensed under the Apache License, Version 2.0 (the "License"); 5 | * you may not use this file except in compliance with the License. 6 | * You may obtain a copy of the License at 7 | * 8 | * http://www.apache.org/licenses/LICENSE-2.0 9 | * 10 | * Unless required by applicable law or agreed to in writing, software 11 | * distributed under the License is distributed on an "AS IS" BASIS, 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 | * See the License for the specific language governing permissions and 14 | * limitations under the License. 15 | */ 16 | 17 | package uk.gov.hmrc.emailaddress 18 | 19 | import org.scalatest.matchers.should.Matchers 20 | import org.scalatest.wordspec.AnyWordSpec 21 | import uk.gov.hmrc.emailaddress.EmailAddress.{Domain, Mailbox} 22 | import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks 23 | 24 | 25 | class EmailAddressSpec extends AnyWordSpec with ScalaCheckPropertyChecks with Matchers with EmailAddressGenerators { 26 | 27 | "Creating an EmailAddress class" should { 28 | "work for a valid email" in { 29 | forAll(validEmailAddresses()) { address => 30 | EmailAddress(address).value should be(address) 31 | } 32 | } 33 | 34 | "throw an exception for an invalid email" in { 35 | an [IllegalArgumentException] should be thrownBy { EmailAddress("sausages") } 36 | } 37 | 38 | "throw an exception for an valid email starting with invalid characters" in { 39 | forAll(validEmailAddresses()) { address => 40 | an [IllegalArgumentException] should be thrownBy { EmailAddress("§"+ address) } 41 | } 42 | } 43 | 44 | "throw an exception for an valid email ending with invalid characters" in { 45 | forAll(validEmailAddresses()) { address => 46 | an [IllegalArgumentException] should be thrownBy { EmailAddress(address + "§") } 47 | } 48 | } 49 | 50 | "throw an exception for an empty email" in { 51 | an [IllegalArgumentException] should be thrownBy { EmailAddress("") } 52 | } 53 | 54 | "throw an exception for a repeated email" in { 55 | an[IllegalArgumentException] should be thrownBy { EmailAddress("test@domain.comtest@domain.com") } 56 | } 57 | 58 | "throw an exception when the '@' is missing" in { 59 | forAll { s: String => whenever(!s.contains("@")) { 60 | an[IllegalArgumentException] should be thrownBy { EmailAddress(s) } 61 | }} 62 | } 63 | } 64 | 65 | "An EmailAddress class" should { 66 | "implicitly convert to a String of the address" in { 67 | val e: String = EmailAddress("test@domain.com") 68 | e should be ("test@domain.com") 69 | } 70 | "toString to a String of the address" in { 71 | val e = EmailAddress("test@domain.com") 72 | e.toString should be ("test@domain.com") 73 | } 74 | "be obfuscatable" in { 75 | EmailAddress("abcdef@example.com").obfuscated.value should be("a****f@example.com") 76 | } 77 | "have a local part" in forAll (validMailbox, validDomain) { (mailbox, domain) => 78 | val exampleAddr = EmailAddress(s"$mailbox@$domain") 79 | exampleAddr.mailbox should (be (a[Mailbox]) and have (Symbol("value") (mailbox))) 80 | exampleAddr.domain should (be (a[Domain]) and have (Symbol("value") (domain))) 81 | } 82 | } 83 | 84 | "A email address domain" should { 85 | "be extractable from an address" in forAll (validMailbox, validDomain) { (mailbox, domain) => 86 | EmailAddress(s"$mailbox@$domain").domain should (be (a[Domain]) and have (Symbol("value") (domain))) 87 | } 88 | "be creatable for a valid domain" in forAll (validDomain) { domain => 89 | EmailAddress.Domain(domain) should (be (a[Domain]) and have (Symbol("value") (domain))) 90 | } 91 | "not create for invalid domains" in { 92 | an [IllegalArgumentException] should be thrownBy EmailAddress.Domain("") 93 | an [IllegalArgumentException] should be thrownBy EmailAddress.Domain("e.") 94 | an [IllegalArgumentException] should be thrownBy EmailAddress.Domain(".uk") 95 | an [IllegalArgumentException] should be thrownBy EmailAddress.Domain(".com") 96 | an [IllegalArgumentException] should be thrownBy EmailAddress.Domain("*domain") 97 | } 98 | "compare equal if identical" in forAll (validDomain, validMailbox, validMailbox) { (domain, mailboxA, mailboxB) => 99 | val exampleA = EmailAddress(s"$mailboxA@$domain") 100 | val exampleB = EmailAddress(s"$mailboxB@$domain") 101 | exampleA.domain should equal (exampleB.domain) 102 | } 103 | "not compare equal if completely different" in forAll (validMailbox, validDomain, validDomain) { (mailbox, domainA, domainB) => 104 | val exampleA = EmailAddress(s"$mailbox@$domainA") 105 | val exampleB = EmailAddress(s"$mailbox@$domainB") 106 | exampleA.domain should not equal exampleB.domain 107 | } 108 | "toString to a String of the domain" in { 109 | Domain("domain.com").toString should be ("domain.com") 110 | } 111 | "implicitly convert to a String of the domain" in { 112 | val e: String = Domain("domain.com") 113 | e should be ("domain.com") 114 | } 115 | } 116 | 117 | "A email address mailbox" should { 118 | 119 | "be extractable from an address" in forAll (validMailbox, validDomain) { (mailbox, domain) => 120 | EmailAddress(s"$mailbox@$domain").mailbox should (be (a[Mailbox]) and have (Symbol("value") (mailbox))) 121 | } 122 | "compare equal" in forAll (validMailbox, validDomain, validDomain) { (mailbox, domainA, domainB) => 123 | val exampleA = EmailAddress(s"$mailbox@$domainA") 124 | val exampleB = EmailAddress(s"$mailbox@$domainB") 125 | exampleA.mailbox should equal (exampleB.mailbox) 126 | } 127 | "not compare equal if completely different" in forAll (validDomain, validMailbox, validMailbox) { (domain, mailboxA, mailboxB) => 128 | val exampleA = EmailAddress(s"$mailboxA@$domain") 129 | val exampleB = EmailAddress(s"$mailboxB@$domain") 130 | exampleA.mailbox should not equal exampleB.mailbox 131 | } 132 | "toString to a String of the domain" in { 133 | EmailAddress("test@domain.com").mailbox.toString should be ("test") 134 | } 135 | "implicitly convert to a String of the domain" in { 136 | val e: String = EmailAddress("test@domain.com").mailbox 137 | e should be ("test") 138 | } 139 | } 140 | } 141 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. --------------------------------------------------------------------------------