diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..0705b0d --- /dev/null +++ b/.dockerignore @@ -0,0 +1,5 @@ +bin/ +build/ +*.db +*.sqlite +.env diff --git a/.env.example b/.env.example index 005eff8..5251402 100644 --- a/.env.example +++ b/.env.example @@ -1,8 +1,8 @@ # Twilio API credentials # (find here https://www.twilio.com/console) -export TWILIO_ACCOUNT_SID= -export TWILIO_AUTH_TOKEN= +TWILIO_ACCOUNT_SID= +TWILIO_AUTH_TOKEN= # Verification Service SID # (create one here https://www.twilio.com/console/verify/services) -export VERIFICATION_SID= +VERIFICATION_SID= diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml new file mode 100644 index 0000000..7e24935 --- /dev/null +++ b/.github/workflows/maven.yml @@ -0,0 +1,34 @@ +# This workflow will build a Java project with Maven +# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven + +name: Java + +on: + push: + branches: [ master, next ] + pull_request: + branches: [ master, next ] + +jobs: + build: + + runs-on: ${{ matrix.platform }} + strategy: + max-parallel: 3 + matrix: + platform: [windows-latest, macos-latest, ubuntu-latest] + + steps: + - uses: actions/checkout@v2 + - name: Set up JDK 11 + uses: actions/setup-java@v1 + with: + java-version: 11.0 + - name: Build with Maven + run: mvn compile + - name: Test + run: mvn clean test + env: + TWILIO_ACCOUNT_SID: ACXXXXXXXX + TWILIO_AUTH_TOKEN: xxxxxxxxxx + VERIFICATION_SID: VAxxxxxxxx diff --git a/.gitignore b/.gitignore index 1569efb..2da9de3 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,9 @@ target/* .env -*.sqlite \ No newline at end of file +*.sqlite + +.classpath +.project +.settings +bin diff --git a/.mergify.yml b/.mergify.yml new file mode 100644 index 0000000..91278e5 --- /dev/null +++ b/.mergify.yml @@ -0,0 +1,10 @@ +pull_request_rules: + - name: automatic merge for Dependabot pull requests + conditions: + - author=dependabot-preview[bot] + - status-success=build (macos-latest) + - status-success=build (windows-latest) + - status-success=build (ubuntu-latest) + actions: + merge: + method: squash diff --git a/CODE_OF_CONDUCT.md b/CODE_OF_CONDUCT.md new file mode 100644 index 0000000..2f0727e --- /dev/null +++ b/CODE_OF_CONDUCT.md @@ -0,0 +1,73 @@ +# Contributor Covenant Code of Conduct + +## Our Pledge + +In the interest of fostering an open and welcoming environment, we as +contributors and maintainers pledge to making participation in our project and +our community a harassment-free experience for everyone, regardless of age, body +size, disability, ethnicity, sex characteristics, gender identity and expression, +level of experience, education, socio-economic status, nationality, personal +appearance, race, religion, or sexual identity and orientation. + +## Our Standards + +Examples of behavior that contributes to creating a positive environment +include: + +- Using welcoming and inclusive language +- Being respectful of differing viewpoints and experiences +- Gracefully accepting constructive criticism +- Focusing on what is best for the community +- Showing empathy towards other community members + +Examples of unacceptable behavior by participants include: + +- The use of sexualized language or imagery and unwelcome sexual attention or + advances +- Trolling, insulting/derogatory comments, and personal or political attacks +- Public or private harassment +- Publishing others' private information, such as a physical or electronic + address, without explicit permission +- Other conduct which could reasonably be considered inappropriate in a + professional setting + +## Our Responsibilities + +Project maintainers are responsible for clarifying the standards of acceptable +behavior and are expected to take appropriate and fair corrective action in +response to any instances of unacceptable behavior. + +Project maintainers have the right and responsibility to remove, edit, or +reject comments, commits, code, wiki edits, issues, and other contributions +that are not aligned to this Code of Conduct, or to ban temporarily or +permanently any contributor for other behaviors that they deem inappropriate, +threatening, offensive, or harmful. + +## Scope + +This Code of Conduct applies both within project spaces and in public spaces +when an individual is representing the project or its community. Examples of +representing a project or community include using an official project e-mail +address, posting via an official social media account, or acting as an appointed +representative at an online or offline event. Representation of a project may be +further defined and clarified by project maintainers. + +## Enforcement + +Instances of abusive, harassing, or otherwise unacceptable behavior may be +reported by contacting the project team at open-source@twilio.com. All +complaints will be reviewed and investigated and will result in a response that +is deemed necessary and appropriate to the circumstances. The project team is +obligated to maintain confidentiality with regard to the reporter of an incident. +Further details of specific enforcement policies may be posted separately. + +Project maintainers who do not follow or enforce the Code of Conduct in good +faith may face temporary or permanent repercussions as determined by other +members of the project's leadership. + +## Attribution + +This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, +available at https://www.contributor-covenant.org/version/1/4/code-of-conduct.html + +[homepage]: https://www.contributor-covenant.org diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..ad3257e --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1,3 @@ +# Contributing to Twilio + +All third party contributors acknowledge that any contributions they provide will be made under the same open source license that the open source project is provided under. diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..923dd05 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,15 @@ +FROM maven:3-openjdk-11 + +WORKDIR /usr/app/ + +RUN apt-get update && \ + apt-get upgrade -y && \ + apt-get install -y build-essential + +COPY . . + +RUN make install + +EXPOSE 8080 + +CMD ["make", "serve"] diff --git a/LICENSE b/LICENSE index 225d6ce..2c3cf9d 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ -The MIT License (MIT) +MIT License -Copyright (c) 2019 Twilio Inc. +Copyright (c) 2020 Twilio Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -18,5 +18,4 @@ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - +SOFTWARE. \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e60c802 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +.PHONY: install serve + +install: + mvn compile flyway:migrate + mvn compile + +serve: + mvn compile jetty:run diff --git a/README.md b/README.md index 528df73..25f85c3 100644 --- a/README.md +++ b/README.md @@ -1,60 +1,129 @@ + +Twilio + + +# Twilio Verify Quickstart with Java servlets + +![](https://github.com/TwilioDevEd/verify-v2-quickstart-java/workflows/Java/badge.svg) + > We are currently in the process of updating this sample template. If you are encountering any issues with the sample, please open an issue at [github.com/twilio-labs/code-exchange/issues](https://github.com/twilio-labs/code-exchange/issues) and we'll try to help you. +## About This application example demonstrates how to Simple phone verification with Java, Servlets, and Twilio Verify. -## Local Development +Implementations in other languages: + +| .NET | Python | Ruby | PHP | Node | +| :--- | :--- | :----- | :-- | :--- | +| [Done](https://github.com/TwilioDevEd/verify-v2-quickstart-csharp) | [Done](https://github.com/TwilioDevEd/verify-v2-quickstart-python) | [Done](https://github.com/TwilioDevEd/verify-v2-quickstart-rails) | [Done](https://github.com/TwilioDevEd/verify-v2-quickstart-php) | [Done](https://github.com/TwilioDevEd/verify-v2-quickstart-node) | + + + +## Set up + +### Requirements -1. First clone this repository and `cd`into it. +- [Java Development Kit](https://adoptopenjdk.net/) version 11 or later. +- A Twilio account - [sign up](https://www.twilio.com/try-twilio) - ```bash - $ git clone git@github.com:TwilioDevEd/verify-v2-quickstart-java.git - $ cd verify-v2-quickstart-java - ``` +### Twilio Account Settings + +This application should give you a ready-made starting point for writing your +own appointment reminder application. Before we begin, we need to collect +all the config values we need to run the application: + +| Config Value | Description | +| :---------------- | :----------------------------------------------------------------------------------------------------------------------------------------------------------- | +| Account Sid | Your primary Twilio account identifier - find this [in the Console](https://www.twilio.com/console). | +| Auth Token | Used to authenticate - [just like the above, you'll find this here](https://www.twilio.com/console). | +| Verification Sid | For Verification Service SID. You can generate one [here](https://www.twilio.com/console/verify/services) | + +### Local development + +After the above requirements have been met: + +1. Clone this repository and `cd` into it + + ```bash + git clone git@github.com:TwilioDevEd/verify-v2-quickstart-java.git + cd verify-v2-quickstart-java + ``` -1. Copy the sample configuration file and edit it to match your configuration. +2. Set your environment variables ```bash - $ cp .env.example .env + cp .env.example .env ``` + See [Twilio Account Settings](#twilio-account-settings) to locate the necessary environment variables. - You'll need to set your `TWILIO_ACCOUNT_SID` and `TWILIO_AUTH_TOKEN` from the - [Twilio Console](https://www.twilio.com/console). Also create a new verification - service [here](https://www.twilio.com/console/verify/services) and set the - service `VERIFICATION_SID` in your `.env`. +3. Build the project - Once you have populated all the values, load the variables with `source`. + ```bash + make install + ``` + **NOTE:** Running the build task will also run the tests + +4. Run the application ```bash - $ source .env + make serve ``` + **NOTE:** If you are using a dedicated Java IDE like Eclipse or IntelliJ, you can start the application within the IDE and it will start in development mode, which means any changes on a source file will be automatically reloaded. - _If you are using a different operating system, make sure that all the variables - from the `.env` file are loaded into your environment._ +5. Navigate to [http://localhost:8080](http://localhost:8080) -1. Execute the migrations. +That's it! - ```bash - $ mvn compile flyway:migrate - ``` +### Docker -1. Run the application. - ```bash - $ mvn compile jetty:run - ``` +If you have [Docker](https://www.docker.com/) already installed on your machine, you can use our `docker-compose.yml` to setup your project. -1. Check it out at [http://localhost:8080/](http://localhost:8080/) +1. Make sure you have the project cloned. +2. Setup the environmental variables in the `docker-compose.yml` file, see the [Twilio Account Settings](#twilio-account-settings). +3. Run `docker-compose --env-file /dev/null up`. +### Tests -## Run the tests +You can run the tests locally by typing: -``` +```bash mvn clean test ``` -## Meta +### Cloud deployment + +Additionally to trying out this application locally, you can deploy it to a variety of host services. Here is a small selection of them. + +Please be aware that some of these might charge you for the usage or might make the source code for this application visible to the public. When in doubt research the respective hosting service first. + +| Service | | +| :-------------------------------- | :------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | +| [Heroku](https://www.heroku.com/) | [![Deploy](https://www.herokucdn.com/deploy/button.svg)](https://heroku.com/deploy?template=https://github.com/TwilioDevEd/verify-v2-quickstart-java/tree/master) | + +**Some notes:** +- For Heroku, please [check this](https://devcenter.heroku.com/articles/deploying-gradle-apps-on-heroku) to properly configure the project for deployment. +- You can also follow [this guide](https://vaadin.com/blog/how-to-deploy-your-java-app-to-the-cloud) to deploy the application to several other cloud services including Google Cloud, Oracle Cloud, etc. + +## Resources + +- The CodeExchange repository can be found [here](https://github.com/twilio-labs/code-exchange/). + +## Contributing + +This template is open source and welcomes contributions. All contributions are subject to our [Code of Conduct](https://github.com/twilio-labs/.github/blob/master/CODE_OF_CONDUCT.md). + +## License + +[MIT](http://www.opensource.org/licenses/mit-license.html) + +## Disclaimer + +No warranty expressed or implied. Software is as is. -* No warranty expressed or implied. Software is as is. Diggity. -* [MIT License](http://www.opensource.org/licenses/mit-license.html) -* Lovingly crafted by Twilio Developer Education. +[twilio]: https://www.twilio.com diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..918f206 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,12 @@ +version: "3.6" +services: + app: + stdin_open: true + tty: true + environment: + - TWILIO_ACCOUNT_SID=your_account_sid + - TWILIO_AUTH_TOKEN=your_account_token + - VERIFICATION_SID=VAXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX + build: . + ports: + - "8080:8080" diff --git a/pom.xml b/pom.xml index 71275c9..6a4959a 100644 --- a/pom.xml +++ b/pom.xml @@ -18,17 +18,17 @@ org.xerial sqlite-jdbc - 3.27.2.1 + 3.30.1 org.eclipse.persistence org.eclipse.persistence.jpa - 2.7.4 + 2.7.6 junit junit - 4.12 + 4.13 test @@ -39,7 +39,7 @@ com.twilio.sdk twilio - 7.37.4 + 7.49.0 org.mindrot @@ -49,9 +49,14 @@ org.mockito mockito-all - 1.10.19 + 2.0.0-beta test + + io.github.cdimascio + dotenv-java + 2.2.0 + diff --git a/src/main/java/com/twilio/verify_quickstart/services/TwilioVerification.java b/src/main/java/com/twilio/verify_quickstart/services/TwilioVerification.java index fcab0be..98d1fbf 100644 --- a/src/main/java/com/twilio/verify_quickstart/services/TwilioVerification.java +++ b/src/main/java/com/twilio/verify_quickstart/services/TwilioVerification.java @@ -4,12 +4,14 @@ import com.twilio.exception.ApiException; import com.twilio.rest.verify.v2.service.Verification; import com.twilio.rest.verify.v2.service.VerificationCheck; +import io.github.cdimascio.dotenv.Dotenv; -public class TwilioVerification implements VerificationService { - private static final String ACCOUNT_SID = System.getenv("TWILIO_ACCOUNT_SID"); - private static final String AUTH_TOKEN = System.getenv("TWILIO_AUTH_TOKEN"); - private static final String VERIFICATION_SID = System.getenv("VERIFICATION_SID"); +public class TwilioVerification implements VerificationService { + private static Dotenv env = Dotenv.configure().ignoreIfMissing().load(); + private static final String ACCOUNT_SID = env.get("TWILIO_ACCOUNT_SID"); + private static final String AUTH_TOKEN = env.get("TWILIO_AUTH_TOKEN"); + private static final String VERIFICATION_SID = env.get("VERIFICATION_SID"); public TwilioVerification() { Twilio.init(ACCOUNT_SID, AUTH_TOKEN); diff --git a/src/main/java/com/twilio/verify_quickstart/servlets/ErrorHandler.java b/src/main/java/com/twilio/verify_quickstart/servlets/ErrorHandler.java new file mode 100644 index 0000000..3c45cf9 --- /dev/null +++ b/src/main/java/com/twilio/verify_quickstart/servlets/ErrorHandler.java @@ -0,0 +1,21 @@ +// Import required java libraries +import java.io.*; +import javax.servlet.*; +import javax.servlet.http.*; +import java.util.*; + +// Extend HttpServlet class +public class ErrorHandler extends HttpServlet { + + // Method to handle GET method request. + public void doGet(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + request.getRequestDispatcher("/error.html").forward(request, response); + } + + // Method to handle POST method request. + public void doPost(HttpServletRequest request, HttpServletResponse response) + throws ServletException, IOException { + doGet(request, response); + } +} diff --git a/src/main/java/com/twilio/verify_quickstart/servlets/VerifyServlet.java b/src/main/java/com/twilio/verify_quickstart/servlets/VerifyServlet.java index 1297d79..fd2aa70 100644 --- a/src/main/java/com/twilio/verify_quickstart/servlets/VerifyServlet.java +++ b/src/main/java/com/twilio/verify_quickstart/servlets/VerifyServlet.java @@ -42,7 +42,7 @@ protected void doPost(HttpServletRequest request, userService.update(user); response.sendRedirect("/"); } else { - request.setAttribute("message",result.getErrors()); + request.setAttribute("message",result.getErrors().toString()); request.getRequestDispatcher("/verify.jsp").forward(request, response); } } diff --git a/src/main/webapp/WEB-INF/web.xml b/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..f870121 --- /dev/null +++ b/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,23 @@ + + + ErrorHandler + ErrorHandler + + + + + ErrorHandler + /ErrorHandler + + + + 404 + /ErrorHandler + + + + java.lang.Throwable + /ErrorHandler + + diff --git a/src/main/webapp/error.html b/src/main/webapp/error.html new file mode 100644 index 0000000..8999c95 --- /dev/null +++ b/src/main/webapp/error.html @@ -0,0 +1,9 @@ + + + + +

Something went wrong!

+

Our Engineers are on it

+ Go Home + +