├── .credo.exs ├── .formatter.exs ├── .github └── workflows │ └── main.yml ├── .gitignore ├── .iex.exs ├── LICENSE ├── README.md ├── config ├── config.exs ├── dev.exs └── test.exs ├── fixture ├── custom_cassettes │ └── client_all.json └── vcr_cassettes │ └── client_all.json ├── lib ├── auth0_ex.ex └── auth0_ex │ ├── api.ex │ ├── application.ex │ ├── authentication.ex │ ├── authentication │ ├── login.ex │ └── token.ex │ ├── config.ex │ ├── management │ ├── blacklist.ex │ ├── client.ex │ ├── client_grant.ex │ ├── connection.ex │ ├── device_credential.ex │ ├── email_provider.ex │ ├── job.ex │ ├── log.ex │ ├── resource_server.ex │ ├── rule.ex │ ├── ticket.ex │ ├── user.ex │ └── user_block.ex │ ├── parser.ex │ ├── token_state.ex │ └── utils.ex ├── mix.exs ├── mix.lock └── test ├── lib └── auth0_ex │ ├── config_test.exs │ └── management │ └── client_test.exs └── test_helper.exs /.credo.exs: -------------------------------------------------------------------------------- 1 | # This file contains the configuration for Credo and you are probably reading 2 | # this after creating it with `mix credo.gen.config`. 3 | # 4 | # If you find anything wrong or unclear in this file, please report an 5 | # issue on GitHub: https://github.com/rrrene/credo/issues 6 | # 7 | %{ 8 | # 9 | # You can have as many configs as you like in the `configs:` field. 10 | configs: [ 11 | %{ 12 | # 13 | # Run any config using `mix credo -C `. If no config name is given 14 | # "default" is used. 15 | # 16 | name: "default", 17 | # 18 | # These are the files included in the analysis: 19 | files: %{ 20 | # 21 | # You can give explicit globs or simply directories. 22 | # In the latter case `**/*.{ex,exs}` will be used. 23 | # 24 | included: [ 25 | "lib/", 26 | "src/", 27 | "test/", 28 | "web/", 29 | "apps/*/lib/", 30 | "apps/*/src/", 31 | "apps/*/test/", 32 | "apps/*/web/" 33 | ], 34 | excluded: [~r"/_build/", ~r"/deps/", ~r"/node_modules/"] 35 | }, 36 | # 37 | # Load and configure plugins here: 38 | # 39 | plugins: [], 40 | # 41 | # If you create your own checks, you must specify the source files for 42 | # them here, so they can be loaded by Credo before running the analysis. 43 | # 44 | requires: [], 45 | # 46 | # If you want to enforce a style guide and need a more traditional linting 47 | # experience, you can change `strict` to `true` below: 48 | # 49 | strict: false, 50 | # 51 | # To modify the timeout for parsing files, change this value: 52 | # 53 | parse_timeout: 5000, 54 | # 55 | # If you want to use uncolored output by default, you can change `color` 56 | # to `false` below: 57 | # 58 | color: true, 59 | # 60 | # You can customize the parameters of any check by adding a second element 61 | # to the tuple. 62 | # 63 | # To disable a check put `false` as second element: 64 | # 65 | # {Credo.Check.Design.DuplicatedCode, false} 66 | # 67 | checks: %{ 68 | enabled: [ 69 | # 70 | ## Consistency Checks 71 | # 72 | {Credo.Check.Consistency.ExceptionNames, []}, 73 | {Credo.Check.Consistency.LineEndings, []}, 74 | {Credo.Check.Consistency.ParameterPatternMatching, []}, 75 | {Credo.Check.Consistency.SpaceAroundOperators, []}, 76 | {Credo.Check.Consistency.SpaceInParentheses, []}, 77 | {Credo.Check.Consistency.TabsOrSpaces, []}, 78 | 79 | # 80 | ## Design Checks 81 | # 82 | # You can customize the priority of any check 83 | # Priority values are: `low, normal, high, higher` 84 | # 85 | {Credo.Check.Design.AliasUsage, 86 | [priority: :low, if_nested_deeper_than: 2, if_called_more_often_than: 0]}, 87 | # You can also customize the exit_status of each check. 88 | # If you don't want TODO comments to cause `mix credo` to fail, just 89 | # set this value to 0 (zero). 90 | # 91 | {Credo.Check.Design.TagTODO, [exit_status: 2]}, 92 | {Credo.Check.Design.TagFIXME, []}, 93 | 94 | # 95 | ## Readability Checks 96 | # 97 | {Credo.Check.Readability.AliasOrder, []}, 98 | {Credo.Check.Readability.FunctionNames, []}, 99 | {Credo.Check.Readability.LargeNumbers, []}, 100 | {Credo.Check.Readability.MaxLineLength, [priority: :low, max_length: 120]}, 101 | {Credo.Check.Readability.ModuleAttributeNames, []}, 102 | {Credo.Check.Readability.ModuleDoc, []}, 103 | {Credo.Check.Readability.ModuleNames, []}, 104 | {Credo.Check.Readability.ParenthesesInCondition, []}, 105 | {Credo.Check.Readability.ParenthesesOnZeroArityDefs, []}, 106 | {Credo.Check.Readability.PipeIntoAnonymousFunctions, []}, 107 | {Credo.Check.Readability.PredicateFunctionNames, []}, 108 | {Credo.Check.Readability.PreferImplicitTry, []}, 109 | {Credo.Check.Readability.RedundantBlankLines, []}, 110 | {Credo.Check.Readability.Semicolons, []}, 111 | {Credo.Check.Readability.SpaceAfterCommas, []}, 112 | {Credo.Check.Readability.StringSigils, []}, 113 | {Credo.Check.Readability.TrailingBlankLine, []}, 114 | {Credo.Check.Readability.TrailingWhiteSpace, []}, 115 | {Credo.Check.Readability.UnnecessaryAliasExpansion, []}, 116 | {Credo.Check.Readability.VariableNames, []}, 117 | {Credo.Check.Readability.WithSingleClause, []}, 118 | 119 | # 120 | ## Refactoring Opportunities 121 | # 122 | {Credo.Check.Refactor.Apply, []}, 123 | {Credo.Check.Refactor.CondStatements, []}, 124 | {Credo.Check.Refactor.CyclomaticComplexity, []}, 125 | {Credo.Check.Refactor.FunctionArity, []}, 126 | {Credo.Check.Refactor.LongQuoteBlocks, []}, 127 | {Credo.Check.Refactor.MatchInCondition, []}, 128 | {Credo.Check.Refactor.MapJoin, []}, 129 | {Credo.Check.Refactor.NegatedConditionsInUnless, []}, 130 | {Credo.Check.Refactor.NegatedConditionsWithElse, []}, 131 | {Credo.Check.Refactor.Nesting, []}, 132 | {Credo.Check.Refactor.UnlessWithElse, []}, 133 | {Credo.Check.Refactor.WithClauses, []}, 134 | {Credo.Check.Refactor.FilterFilter, []}, 135 | {Credo.Check.Refactor.RejectReject, []}, 136 | {Credo.Check.Refactor.RedundantWithClauseResult, []}, 137 | 138 | # 139 | ## Warnings 140 | # 141 | {Credo.Check.Warning.ApplicationConfigInModuleAttribute, []}, 142 | {Credo.Check.Warning.BoolOperationOnSameValues, []}, 143 | {Credo.Check.Warning.ExpensiveEmptyEnumCheck, []}, 144 | {Credo.Check.Warning.IExPry, []}, 145 | {Credo.Check.Warning.IoInspect, []}, 146 | {Credo.Check.Warning.OperationOnSameValues, []}, 147 | {Credo.Check.Warning.OperationWithConstantResult, []}, 148 | {Credo.Check.Warning.RaiseInsideRescue, []}, 149 | {Credo.Check.Warning.SpecWithStruct, []}, 150 | {Credo.Check.Warning.WrongTestFileExtension, []}, 151 | {Credo.Check.Warning.UnusedEnumOperation, []}, 152 | {Credo.Check.Warning.UnusedFileOperation, []}, 153 | {Credo.Check.Warning.UnusedKeywordOperation, []}, 154 | {Credo.Check.Warning.UnusedListOperation, []}, 155 | {Credo.Check.Warning.UnusedPathOperation, []}, 156 | {Credo.Check.Warning.UnusedRegexOperation, []}, 157 | {Credo.Check.Warning.UnusedStringOperation, []}, 158 | {Credo.Check.Warning.UnusedTupleOperation, []}, 159 | {Credo.Check.Warning.UnsafeExec, []} 160 | ], 161 | disabled: [ 162 | # 163 | # Checks scheduled for next check update (opt-in for now, just replace `false` with `[]`) 164 | 165 | # 166 | # Controversial and experimental checks (opt-in, just move the check to `:enabled` 167 | # and be sure to use `mix credo --strict` to see low priority checks) 168 | # 169 | {Credo.Check.Consistency.MultiAliasImportRequireUse, []}, 170 | {Credo.Check.Consistency.UnusedVariableNames, []}, 171 | {Credo.Check.Design.DuplicatedCode, []}, 172 | {Credo.Check.Design.SkipTestWithoutComment, []}, 173 | {Credo.Check.Readability.AliasAs, []}, 174 | {Credo.Check.Readability.BlockPipe, []}, 175 | {Credo.Check.Readability.ImplTrue, []}, 176 | {Credo.Check.Readability.MultiAlias, []}, 177 | {Credo.Check.Readability.NestedFunctionCalls, []}, 178 | {Credo.Check.Readability.SeparateAliasRequire, []}, 179 | {Credo.Check.Readability.SingleFunctionToBlockPipe, []}, 180 | {Credo.Check.Readability.SinglePipe, []}, 181 | {Credo.Check.Readability.Specs, []}, 182 | {Credo.Check.Readability.StrictModuleLayout, []}, 183 | {Credo.Check.Readability.WithCustomTaggedTuple, []}, 184 | {Credo.Check.Refactor.ABCSize, []}, 185 | {Credo.Check.Refactor.AppendSingleItem, []}, 186 | {Credo.Check.Refactor.DoubleBooleanNegation, []}, 187 | {Credo.Check.Refactor.FilterReject, []}, 188 | {Credo.Check.Refactor.IoPuts, []}, 189 | {Credo.Check.Refactor.MapMap, []}, 190 | {Credo.Check.Refactor.ModuleDependencies, []}, 191 | {Credo.Check.Refactor.NegatedIsNil, []}, 192 | {Credo.Check.Refactor.PipeChainStart, []}, 193 | {Credo.Check.Refactor.RejectFilter, []}, 194 | {Credo.Check.Refactor.VariableRebinding, []}, 195 | {Credo.Check.Warning.LazyLogging, []}, 196 | {Credo.Check.Warning.LeakyEnvironment, []}, 197 | {Credo.Check.Warning.MapGetUnsafePass, []}, 198 | {Credo.Check.Warning.MixEnv, []}, 199 | {Credo.Check.Warning.UnsafeToAtom, []} 200 | 201 | # {Credo.Check.Refactor.MapInto, []}, 202 | 203 | # 204 | # Custom checks can be created using `mix credo.gen.check`. 205 | # 206 | ] 207 | } 208 | } 209 | ] 210 | } 211 | -------------------------------------------------------------------------------- /.formatter.exs: -------------------------------------------------------------------------------- 1 | # Used by "mix format" 2 | [ 3 | inputs: ["mix.exs", "{config,lib,test}/**/*.{ex,exs}"] 4 | ] 5 | -------------------------------------------------------------------------------- /.github/workflows/main.yml: -------------------------------------------------------------------------------- 1 | name: CI 2 | 3 | on: 4 | push: 5 | branches: 6 | - "*" 7 | 8 | jobs: 9 | lint: 10 | runs-on: ${{ matrix.os }} 11 | env: 12 | MIX_ENV: dev 13 | name: Lint 14 | strategy: 15 | matrix: 16 | os: ["ubuntu-20.04"] 17 | elixir: ["1.13"] 18 | otp: ["24"] 19 | steps: 20 | - uses: actions/checkout@v2 21 | - uses: erlef/setup-beam@v1 22 | with: 23 | otp-version: ${{ matrix.otp }} 24 | elixir-version: ${{ matrix.elixir }} 25 | - uses: actions/cache@v2 26 | with: 27 | path: deps 28 | key: ${{ matrix.os }}-otp_${{ matrix.otp }}-elixir_${{ matrix.elixir }}-mix_${{ hashFiles('**/mix.lock') }} 29 | restore-keys: ${{ matrix.os }}-otp_${{ matrix.otp }}-elixir_${{ matrix.elixir }}-mix_ 30 | - run: mix deps.get 31 | - run: mix deps.compile 32 | - run: mix format --check-formatted 33 | - run: mix deps.unlock --check-unused 34 | - run: mix credo --strict --all 35 | 36 | test: 37 | runs-on: ${{ matrix.os }} 38 | name: Test Elixir ${{ matrix.elixir }}, OTP ${{ matrix.otp }}, OS ${{ matrix.os }} 39 | env: 40 | MIX_ENV: test 41 | strategy: 42 | fail-fast: false 43 | matrix: 44 | os: ["ubuntu-20.04"] 45 | elixir: ["1.13", "1.12", "1.11"] 46 | otp: ["24", "23", "22"] 47 | steps: 48 | - uses: actions/checkout@v2 49 | - uses: erlef/setup-beam@v1 50 | with: 51 | otp-version: ${{ matrix.otp }} 52 | elixir-version: ${{ matrix.elixir }} 53 | - uses: actions/cache@v2 54 | with: 55 | path: deps 56 | key: ${{ matrix.os }}-otp_${{ matrix.otp }}-elixir_${{ matrix.elixir }}-mix_${{ hashFiles('**/mix.lock') }} 57 | restore-keys: ${{ matrix.os }}-otp_${{ matrix.otp }}-elixir_${{ matrix.elixir }}-mix_ 58 | - run: mix deps.get --only test 59 | - run: mix deps.compile 60 | - run: mix compile 61 | - run: mix test 62 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # The directory Mix will write compiled artifacts to. 2 | /_build 3 | 4 | # If you run "mix test --cover", coverage assets end up here. 5 | /cover 6 | 7 | # The directory Mix downloads your dependencies sources to. 8 | /deps 9 | 10 | # Where 3rd-party dependencies like ExDoc output generated docs. 11 | /doc 12 | 13 | # If the VM crashes, it generates a dump, let's ignore it too. 14 | erl_crash.dump 15 | 16 | # Also ignore archive artifacts (built via "mix archive.build"). 17 | *.ez 18 | 19 | # runner file that I use for my work 20 | runner 21 | -------------------------------------------------------------------------------- /.iex.exs: -------------------------------------------------------------------------------- 1 | alias Auth0Ex.TokenState 2 | alias Auth0Ex.Management.Client 3 | alias Auth0Ex.Management.User 4 | -------------------------------------------------------------------------------- /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 2022 Samar Acharya 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. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Auth0Ex 2 | 3 | [![Hex version](https://img.shields.io/hexpm/v/auth0_ex.svg "Hex version")](https://hex.pm/packages/auth0_ex) ![Hex downloads](https://img.shields.io/hexpm/dt/auth0_ex.svg "Hex downloads") ![Build Status](https://github.com/techgaun/auth0_ex/actions/workflows/main.yml/badge.svg) 4 | 5 | > An elixir client library for Auth0 6 | 7 | ## Installation 8 | 9 | You can install the stable version from hex: 10 | 11 | ```elixir 12 | def deps do 13 | [{:auth0_ex, "~> 0.9"}] 14 | end 15 | ``` 16 | 17 | You can use github repo as your package source to use the latest source code but beware it might have breaking and unstable code: 18 | 19 | ```elixir 20 | def deps do 21 | [{:auth0_ex, github: "techgaun/auth0_ex"}] 22 | end 23 | ``` 24 | ## Configuration 25 | 26 | Add a configuration block like below: 27 | 28 | - First option is to use management api client. 29 | 30 | You can create non-interactive client for management api as described [HERE](https://auth0.com/docs/api/management/v2/tokens). 31 | Once you do that, you should be able to grab client ID and client secret from the setting page for use with Auth0Ex. 32 | This is a recommended setup as Auth0Ex can recreate new `access_token` when the current one expires. 33 | 34 | ```elixir 35 | config :auth0_ex, 36 | domain: System.get_env("AUTH0_DOMAIN"), 37 | mgmt_client_id: System.get_env("AUTH0_MGMT_CLIENT_ID"), 38 | mgmt_client_secret: System.get_env("AUTH0_MGMT_CLIENT_SECRET"), 39 | http_opts: [] 40 | ``` 41 | 42 | - Second option is to use pre-created token with access to management API. 43 | 44 | ```elixir 45 | config :auth0_ex, 46 | domain: System.get_env("AUTH0_DOMAIN"), 47 | mgmt_token: System.get_env("AUTH0_MGMT_TOKEN"), 48 | http_opts: [] 49 | ``` 50 | 51 | ### Notes 52 | 53 | - The v2 `search_engine` API is being deprecated by Auth0. User queries now specify `search_engine=v3` by default. If for some reason you need the `v2` engine you can set `v2_search: true` in your config block. 54 | - if you use pre-created management token, it will always be used for your requests. 55 | - `AUTH0_DOMAIN` should be entire tenant domain Auth0 provides. We fall back to adding `auth0.com` right now but that will be removed in future version. This allows us to use `Auth0Ex` in all tenant regions unlike the previous versions. 56 | - If you don't want `auth0.com` to be added, you can set `config :auth0_ex, custom_domain: true` 57 | 58 | Export appropriate environment variable and you should be all set. 59 | 60 | Please refer to the [documentation](https://hexdocs.pm/auth0_ex/) for more details. 61 | 62 | ### Authentication API 63 | 64 | In addition to the management API resources, there is also a support for most used authentication APIs. 65 | Authentication API will use the same `domain` config you specify in the config above. If you wish to use `Auth0Ex` 66 | for authentication APIs only, all you need to specify is `domain` in config. 67 | 68 | ## Author 69 | 70 | - [techgaun](https://github.com/techgaun) 71 | -------------------------------------------------------------------------------- /config/config.exs: -------------------------------------------------------------------------------- 1 | # This file is responsible for configuring your application 2 | # and its dependencies with the aid of the Mix.Config module. 3 | import Config 4 | import_config "#{Mix.env()}.exs" 5 | -------------------------------------------------------------------------------- /config/dev.exs: -------------------------------------------------------------------------------- 1 | import Config 2 | 3 | config :auth0_ex, 4 | domain: System.get_env("AUTH0_DOMAIN"), 5 | mgmt_token: System.get_env("AUTH0_MGMT_TOKEN"), 6 | mgmt_client_id: System.get_env("AUTH0_MGMT_CLIENT_ID"), 7 | mgmt_client_secret: System.get_env("AUTH0_MGMT_CLIENT_SECRET"), 8 | http_opts: [] 9 | -------------------------------------------------------------------------------- /config/test.exs: -------------------------------------------------------------------------------- 1 | import Config 2 | 3 | config :auth0_ex, 4 | domain: "brighterlink.auth0.com", 5 | mgmt_token: "TESTTOKEN", 6 | http_opts: [] 7 | 8 | config :exvcr, 9 | vcr_cassette_library_dir: "fixture/vcr_cassettes", 10 | custom_cassette_library_dir: "fixture/custom_cassettes", 11 | filter_request_headers: ["Authorization"] 12 | -------------------------------------------------------------------------------- /fixture/custom_cassettes/client_all.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "name": "My application", 4 | "client_id": "AaiyAPdpYdesoKnqjj8HJqRn4T5titww", 5 | "client_secret": "MG_TNT2ver-SylNat-_VeMmd-4m0Waba0jr1troztBniSChEw0glxEmgEi2Kw40H", 6 | "app_type": "", 7 | "logo_uri": "", 8 | "is_first_party": false, 9 | "oidc_conformant": false, 10 | "callbacks": [ 11 | "http://localhost/callback" 12 | ], 13 | "allowed_origins": [ 14 | "" 15 | ], 16 | "client_aliases": [ 17 | "" 18 | ], 19 | "allowed_clients": [ 20 | "" 21 | ], 22 | "allowed_logout_urls": [ 23 | "http://localhost/logoutCallback" 24 | ], 25 | "jwt_configuration": { 26 | "lifetime_in_seconds": 36000, 27 | "secret_encoded": true, 28 | "scopes": {}, 29 | "alg": "HS256" 30 | }, 31 | "signing_keys": [ 32 | "object" 33 | ], 34 | "encryption_key": { 35 | "pub": "", 36 | "cert": "", 37 | "subject": "" 38 | }, 39 | "sso": false, 40 | "sso_disabled": false, 41 | "custom_login_page_on": true, 42 | "custom_login_page": "", 43 | "custom_login_page_preview": "", 44 | "form_template": "", 45 | "addons": {}, 46 | "token_endpoint_auth_method": "none", 47 | "resource_servers": [ 48 | { 49 | "identifier": "api.tenant.com", 50 | "scopes": [ 51 | "read:something" 52 | ] 53 | } 54 | ], 55 | "client_metadata": {}, 56 | "mobile": { 57 | "android": { 58 | "app_package_name": "com.example", 59 | "sha256_cert_fingerprints": [ 60 | "D8:A0:83:..." 61 | ] 62 | }, 63 | "ios": { 64 | "team_id": "9JA89QQLNQ", 65 | "app_bundle_identifier": "com.my.bundle.id" 66 | } 67 | } 68 | } 69 | ] 70 | -------------------------------------------------------------------------------- /fixture/vcr_cassettes/client_all.json: -------------------------------------------------------------------------------- 1 | [ 2 | { 3 | "request": { 4 | "body": "", 5 | "headers": { 6 | "User-Agent": "Auth0Ex ", 7 | "Authorization": "***", 8 | "Content-Type": "application/json" 9 | }, 10 | "method": "get", 11 | "options": [], 12 | "request_body": "", 13 | "url": "https://brighterlink.auth0.com/api/v2/clients?fields=name" 14 | }, 15 | "response": { 16 | "body": "[{\"name\":\"Samar Staging\"},{\"name\":\"Samar Dev\"},{\"name\":\"Samar\"},{\"name\":\"Auth0 Management API (Test Client)\"},{\"name\":\"Samar Present\"},{\"name\":\"All Applications\"}]", 17 | "headers": { 18 | "Date": "Thu, 12 Jan 2017 07:56:34 GMT", 19 | "Content-Type": "application/json", 20 | "Content-Length": "191", 21 | "Connection": "keep-alive", 22 | "Keep-Alive": "timeout=100", 23 | "x-ratelimit-limit": "50", 24 | "x-ratelimit-remaining": "49", 25 | "x-ratelimit-reset": "1484207795", 26 | "vary": "origin", 27 | "cache-control": "no-cache", 28 | "accept-ranges": "bytes", 29 | "Strict-Transport-Security": "max-age=15724800", 30 | "X-Robots-Tag": "noindex, nofollow, nosnippet, noarchive" 31 | }, 32 | "status_code": 200, 33 | "type": "ok" 34 | } 35 | } 36 | ] 37 | -------------------------------------------------------------------------------- /lib/auth0_ex.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex do 2 | @moduledoc """ 3 | Auth0Ex is an API client that supports both authentication and management API of Auth0. 4 | 5 | Auth0Ex has been built to mostly satisfy the need for calling various management API 6 | for the backend services. 7 | """ 8 | end 9 | -------------------------------------------------------------------------------- /lib/auth0_ex/api.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Api do 2 | @moduledoc """ 3 | The main API module supposed to be used by individual authentication and management API modules 4 | 5 | Example Usage would look something like below: 6 | 7 | use Auth0Ex.Api 8 | use Auth0Ex.Api, for: :mgmt 9 | """ 10 | 11 | defmacro __using__(opts) do 12 | quote location: :keep do 13 | alias Auth0Ex.Parser 14 | import Auth0Ex.Utils 15 | 16 | @doc false 17 | def build_url(path, params) do 18 | "#{base_url(unquote(opts)[:for])}#{path}?#{query_string(params)}" 19 | end 20 | 21 | @doc false 22 | def do_get(path, params) when is_map(params) do 23 | do_request(:get, path, params, "") 24 | end 25 | 26 | @doc false 27 | def do_get(path, params) when is_list(params) do 28 | do_get(path, Enum.into(params, %{})) 29 | end 30 | 31 | def do_post(path, body \\ %{}) 32 | 33 | @doc false 34 | def do_post(path, body) when is_list(body) do 35 | do_post(path, Enum.into(body, %{})) 36 | end 37 | 38 | @doc false 39 | def do_post(path, body) do 40 | do_request(:post, path, %{}, Jason.encode!(body)) 41 | end 42 | 43 | @doc false 44 | def do_put(path, body \\ %{}) do 45 | do_request(:put, path, %{}, Jason.encode!(body)) 46 | end 47 | 48 | def do_patch(path, body \\ %{}) 49 | 50 | @doc false 51 | def do_patch(path, body) when is_list(body) do 52 | do_patch(path, Enum.into(body, %{})) 53 | end 54 | 55 | @doc false 56 | def do_patch(path, body) do 57 | do_request(:patch, path, %{}, Jason.encode!(body)) 58 | end 59 | 60 | @doc false 61 | def do_delete(path) do 62 | do_request(:delete, path) 63 | end 64 | 65 | @doc false 66 | def do_delete(path, params) do 67 | do_request(:delete, path, params) 68 | end 69 | 70 | defp do_request(method, path, params \\ %{}, req_body \\ "") do 71 | uri = build_url(path, params) 72 | 73 | method 74 | |> HTTPoison.request(uri, req_body, req_header(unquote(opts[:for])), http_opts()) 75 | |> Parser.parse() 76 | end 77 | 78 | defp query_string(params) do 79 | default_params() 80 | |> Map.merge(params) 81 | |> URI.encode_query() 82 | end 83 | 84 | defp default_params do 85 | %{} 86 | end 87 | 88 | defoverridable default_params: 0 89 | end 90 | end 91 | end 92 | -------------------------------------------------------------------------------- /lib/auth0_ex/application.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Application do 2 | # See http://elixir-lang.org/docs/stable/elixir/Application.html 3 | # for more information on OTP Applications 4 | @moduledoc false 5 | 6 | use Application 7 | 8 | def start(_type, _args) do 9 | import Supervisor.Spec, warn: false 10 | 11 | # Define workers and child supervisors to be supervised 12 | children = [ 13 | # Starts a worker by calling: Auth0Ex.Worker.start_link(arg1, arg2, arg3) 14 | # {Auth0Ex.Worker, [arg1, arg2, arg3]}, 15 | 16 | {Auth0Ex.TokenState, []} 17 | ] 18 | 19 | # See http://elixir-lang.org/docs/stable/elixir/Supervisor.html 20 | # for other strategies and supported options 21 | opts = [strategy: :one_for_one, name: Auth0Ex.Supervisor] 22 | Supervisor.start_link(children, opts) 23 | end 24 | end 25 | -------------------------------------------------------------------------------- /lib/auth0_ex/authentication.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Authentication do 2 | @moduledoc """ 3 | A module with various functions related to authentication api for Auth0. 4 | 5 | Unlike the management API code, most of the authentication code 6 | sit in this module as it makes sense for management API given the 7 | resourceful nature of each object but as useful for Authentication APIs. 8 | """ 9 | use Auth0Ex.Api 10 | @db_conn_path "dbconnections" 11 | 12 | @doc """ 13 | Given a user's credentials, and a connection, this endpoint will create 14 | a new user using active authentication. This endpoint only works for database connections. 15 | 16 | API Management https://auth0.com/docs/api/authentication#signup 17 | 18 | client_id: is part of your configuration and you should get via `Application.get_env/2` 19 | connection: is an auth0 concept; They list the following connections `database, social, enterprise, or passwordless connection`. 20 | 21 | A use case should be, we want to use the database connection to create new signup, the value for that connection is in the dashboard inside of `Connections -> Database` or at the URL `https://manage.auth0.com/dashboard/us//connections/database`. 22 | 23 | NOTE: The default name for the database connection is `Username-Password-Authentication`. You can create a new database connection instead. 24 | 25 | iex> client_id = Application.get_env(:auth0_ex, :mgmt_client_id) 26 | iex> connection = "Username-Password-Authentication" 27 | iex> Auth0Ex.Authentication.signup(client_id, "samar@example.com", "password", connection, %{user_metadata: %{plan: "silver", team_id: "a111"}}) 28 | """ 29 | def signup(client_id, email, password, connection, extra_params \\ %{}) do 30 | payload = %{ 31 | client_id: client_id, 32 | email: email, 33 | password: password, 34 | connection: connection 35 | } 36 | 37 | do_post("#{@db_conn_path}/signup", Map.merge(payload, extra_params)) 38 | end 39 | 40 | @doc """ 41 | Given a user's email address and a connection, Auth0 will send a change password email. 42 | This endpoint only works for database connections. 43 | 44 | https://auth0.com/docs/api/authentication#change-password 45 | 46 | iex> client_id = Application.get_env(:auth0_ex, :mgmt_client_id) 47 | iex> connection = "Username-Password-Authentication" 48 | iex> Auth0Ex.Authentication.change_password(client_id, "samar@example.com", connection) 49 | """ 50 | def change_password(client_id, email, connection) do 51 | payload = %{ 52 | client_id: client_id, 53 | email: email, 54 | connection: connection 55 | } 56 | 57 | do_post("#{@db_conn_path}/change_password", payload) 58 | end 59 | 60 | @doc """ 61 | Given the Auth0 access token obtained during login, this endpoint returns a user's profile. 62 | This endpoint will work only if `openid` was granted as a scope for the `access_token`. 63 | 64 | iex> Auth0Ex.Authentication.userinfo("sample.access_token.here") 65 | """ 66 | def userinfo(access_token) do 67 | do_get("userinfo", %{access_token: access_token}) 68 | end 69 | 70 | @doc """ 71 | This endpoint validates a JSON Web Token (signature and expiration) and returns the 72 | user information associated with the user id sub property of the token. 73 | 74 | iex> Auth0Ex.Authentication.tokeninfo("sample.id_token.here") 75 | """ 76 | def tokeninfo(id_token) do 77 | do_post("tokeninfo", %{id_token: id_token}) 78 | end 79 | end 80 | -------------------------------------------------------------------------------- /lib/auth0_ex/authentication/login.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Authentication.Login do 2 | @moduledoc """ 3 | A module that handles login stuff for authentication API 4 | """ 5 | 6 | use Auth0Ex.Api 7 | 8 | @doc """ 9 | Given the social provider's access_token and the connection, this endpoint will authenticate 10 | the user with the provider and return a JSON with the `access_token` and, optionally, an `id_token`. 11 | This endpoint only works for Facebook, Google, Twitter and Weibo. 12 | 13 | iex> Auth0Ex.Authentication.Login.social("client_id", "access_token", "facebook") 14 | iex> Auth0Ex.Authentication.Login.social("client_id", "access_token", "facebook", "openid profile email") 15 | """ 16 | def social(client_id, access_token, connection, scope \\ "openid") do 17 | payload = %{ 18 | client_id: client_id, 19 | access_token: access_token, 20 | connection: connection, 21 | scope: scope 22 | } 23 | 24 | do_post("oauth/access_token", payload) 25 | end 26 | 27 | @doc """ 28 | Given the user credentials and the connection specified, it will do the authentication 29 | on the provider and return a JSON with the `access_token` and `id_token`. 30 | 31 | You can pass optional parameters as a map in the last `params` argument. 32 | 33 | An example params map would be: 34 | 35 | %{scope: "openid app_metadata"} 36 | %{device: "ios", id_token: "id_token_touchid"} 37 | 38 | 39 | iex> Auth0Ex.Authentication.Login.database("client_id", "samar", "samarpwd", "dev", "password") 40 | iex> Auth0Ex.Authentication.Login.database("client_id", "samar", "samarpwd", "dev", "password", %{scope: "openid app_metadata"}) 41 | """ 42 | def database(client_id, username, password, connection, grant_type, params \\ %{}) 43 | when is_map(params) do 44 | payload = %{ 45 | client_id: client_id, 46 | username: username, 47 | password: password, 48 | connection: connection, 49 | grant_type: grant_type 50 | } 51 | 52 | do_post("oauth/ro", Map.merge(payload, params)) 53 | end 54 | end 55 | -------------------------------------------------------------------------------- /lib/auth0_ex/authentication/token.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Authentication.Token do 2 | @moduledoc """ 3 | A moodule for getting tokens for authentication. 4 | This is the module that gives you auth codes and client credentials. 5 | 6 | More information at [this page](https://auth0.com/docs/api/authentication?http#get-token) 7 | """ 8 | 9 | use Auth0Ex.Api 10 | @path "oauth/token" 11 | 12 | @doc """ 13 | This is the OAuth 2.0 grant that regular web apps utilize in order to 14 | access an API. Use this endpoint to exchange an Authorization Code for a Token. 15 | 16 | iex> Auth0Ex.Authentication.Token.auth_code("client_id", "client_secret", "code") 17 | iex> Auth0Ex.Authentication.Token.auth_code("client_id", "client_secret", "code", "redirect_uri_here") 18 | """ 19 | def auth_code(client_id, client_secret, code, redirect_uri \\ nil) do 20 | payload = %{ 21 | client_id: client_id, 22 | client_secret: client_secret, 23 | code: code, 24 | redirect_uri: redirect_uri, 25 | grant_type: "authorization_code" 26 | } 27 | 28 | do_post(@path, payload) 29 | end 30 | 31 | @doc """ 32 | This is the OAuth 2.0 grant that mobile apps utilize in order to 33 | access an API. Use this endpoint to exchange an Authorization Code for a Token. 34 | iex> Auth0Ex.Authentication.Token.auth_code_pkce("client_id", "client_secret", "code", "code_verifier") 35 | iex> Auth0Ex.Authentication.Token.auth_code_pkce("client_id", "client_secret", "code", "code_verifier", "redirect_uri_here") 36 | """ 37 | def auth_code_pkce(client_id, client_secret, code, code_verifier, redirect_uri \\ nil) do 38 | payload = %{ 39 | client_id: client_id, 40 | client_secret: client_secret, 41 | code: code, 42 | code_verifier: code_verifier, 43 | redirect_uri: redirect_uri, 44 | grant_type: "authorization_code" 45 | } 46 | 47 | do_post(@path, payload) 48 | end 49 | 50 | @doc """ 51 | This is the OAuth 2.0 grant that server processes utilize in order to access an API. 52 | Use this endpoint to directly request an access_token by using the Client Credentials 53 | (a Client Id and a Client Secret). 54 | 55 | iex> Auth0Ex.Authentication.Token.client_credentials("client_id", "client_secret", "API_IDENTIFIER_aud") 56 | """ 57 | def client_credentials(client_id, client_secret, audience) do 58 | payload = %{ 59 | client_id: client_id, 60 | client_secret: client_secret, 61 | audience: audience, 62 | grant_type: "client_credentials" 63 | } 64 | 65 | do_post(@path, payload) 66 | end 67 | end 68 | -------------------------------------------------------------------------------- /lib/auth0_ex/config.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Config do 2 | @moduledoc """ 3 | All of the configurations pertaining to `Auth0Ex`. 4 | """ 5 | 6 | @spec domain() :: String.t() 7 | def domain do 8 | auth0_domain = Application.get_env(:auth0_ex, :domain) 9 | 10 | if String.ends_with?(auth0_domain, "auth0.com") or custom_domain?() do 11 | auth0_domain 12 | else 13 | raise ArgumentError, 14 | "the domain specified must be a fully qualified domain name, it should be : [.optional_region].auth0.com, not just " 15 | end 16 | end 17 | 18 | @spec mgmt_client_id() :: String.t() | nil 19 | def mgmt_client_id do 20 | Application.get_env(:auth0_ex, :mgmt_client_id) 21 | end 22 | 23 | @spec mgmt_client_secret() :: String.t() | nil 24 | def mgmt_client_secret do 25 | Application.get_env(:auth0_ex, :mgmt_client_secret) 26 | end 27 | 28 | @spec mgmt_token() :: String.t() | nil 29 | def mgmt_token do 30 | Application.get_env(:auth0_ex, :mgmt_token) 31 | end 32 | 33 | @spec http_opts() :: list() 34 | def http_opts do 35 | Application.get_env(:auth0_ex, :http_opts) || [] 36 | end 37 | 38 | @spec user_agent() :: String.t() 39 | def user_agent do 40 | Application.get_env(:auth0_ex, :user_agent) || 41 | "Auth0Ex " 42 | end 43 | 44 | defp custom_domain? do 45 | Application.get_env(:auth0_ex, :custom_domain, false) in [true, "true"] 46 | end 47 | end 48 | -------------------------------------------------------------------------------- /lib/auth0_ex/management/blacklist.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Management.Blacklist do 2 | @moduledoc """ 3 | A module representing blacklisted tokens on Auth0 4 | """ 5 | use Auth0Ex.Api, for: :mgmt 6 | @path "blacklists/tokens" 7 | 8 | @doc """ 9 | Retrieves the `jti` and `aud` of all tokens in the blacklist. 10 | 11 | iex> Auth0Ex.Management.Blacklist.get("some_aud") 12 | """ 13 | def get(aud) do 14 | do_get(@path, aud: aud) 15 | end 16 | 17 | @doc """ 18 | Adds the token identified by the `jti` to a blacklist for the tenant. 19 | 20 | iex> Auth0Ex.Management.Blacklist.blacklist(aud: "some_aud", jti: "some_jti") 21 | """ 22 | def blacklist(body) do 23 | do_post(@path, body) 24 | end 25 | end 26 | -------------------------------------------------------------------------------- /lib/auth0_ex/management/client.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Management.Client do 2 | @moduledoc """ 3 | A module representing client on Auth0 4 | """ 5 | 6 | use Auth0Ex.Api, for: :mgmt 7 | @path "clients" 8 | 9 | @doc """ 10 | Gets all the clients 11 | 12 | iex> Auth0Ex.Management.Client.all() 13 | iex> Auth0Ex.Management.Client.all(fields: "name,client_id") 14 | """ 15 | def all(params \\ %{}) when is_map(params) or is_list(params) do 16 | do_get(@path, params) 17 | end 18 | 19 | @doc """ 20 | Gets the information for individual client 21 | 22 | iex> Auth0Ex.Management.Client.get("some_id") 23 | iex> Auth0Ex.Management.Client.get("some_id", [fields: "id,client_id", include_fields: true]) 24 | """ 25 | def get(id) when is_binary(id), do: get(id, []) 26 | 27 | def get(id, params) when is_binary(id) and (is_map(params) or is_list(params)) do 28 | do_get("#{@path}/#{id}", params) 29 | end 30 | 31 | @doc """ 32 | Creates a new Auth0 client from given body 33 | 34 | iex> Auth0Ex.Management.Client.create(%{name: "Samars App"}) 35 | """ 36 | def create(body) do 37 | do_post(@path, body) 38 | end 39 | 40 | @doc """ 41 | Updates Auth0 client of given ID with given body 42 | 43 | iex> Auth0Ex.Management.Client.update("some_client_id", %{name: "New Client Name"}) 44 | """ 45 | def update(id, body) do 46 | do_patch("#{@path}/#{id}", body) 47 | end 48 | 49 | @doc """ 50 | Deletes the client with given client id 51 | 52 | iex> Auth0Ex.Management.Client.delete("some_client_id") 53 | """ 54 | def delete(id) do 55 | do_delete("#{@path}/#{id}") 56 | end 57 | 58 | @doc """ 59 | Rotate a client secret for client with given ID 60 | 61 | iex> Auth0Ex.Management.Client.rotate_secret("some_client_id") 62 | """ 63 | def rotate_secret(id) do 64 | do_post("#{@path}/#{id}/rotate-secret") 65 | end 66 | end 67 | -------------------------------------------------------------------------------- /lib/auth0_ex/management/client_grant.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Management.ClientGrant do 2 | @moduledoc """ 3 | A module representing client_grant resource 4 | """ 5 | use Auth0Ex.Api, for: :mgmt 6 | @path "client-grants" 7 | 8 | @doc """ 9 | Gets all the client grants 10 | 11 | iex> Auth0Ex.Management.ClientGrant.all() 12 | iex> Auth0Ex.Management.ClientGrant.all(audience: "aud") 13 | """ 14 | def all(params \\ %{}) when is_map(params) or is_list(params) do 15 | do_get(@path, params) 16 | end 17 | 18 | @doc """ 19 | Creates a client grant 20 | 21 | iex> Auth0Ex.Management.ClientGrant.create(%{client_id: "client_id", audience: "someaud", scope: []}) 22 | """ 23 | def create(body) do 24 | do_post(@path, body) 25 | end 26 | 27 | @doc """ 28 | Updates a client grant 29 | 30 | iex> Auth0Ex.Management.ClientGrant.update("client_grant_to_update", %{scope: []}) 31 | """ 32 | def update(id, body) do 33 | do_patch("#{@path}/#{id}", body) 34 | end 35 | 36 | @doc """ 37 | Delete a client grant 38 | 39 | iex> Auth0Ex.Management.ClientGrant.delete("client_grant_to_delete") 40 | """ 41 | def delete(id) do 42 | do_delete("#{@path}/#{id}") 43 | end 44 | end 45 | -------------------------------------------------------------------------------- /lib/auth0_ex/management/connection.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Management.Connection do 2 | @moduledoc """ 3 | A module representing connection resource 4 | """ 5 | use Auth0Ex.Api, for: :mgmt 6 | @path "connections" 7 | 8 | @doc """ 9 | Gets all the connections 10 | 11 | iex> Auth0Ex.Management.Connection.all() 12 | iex> Auth0Ex.Management.Connection.all(%{name: "some_name"}) 13 | """ 14 | def all(params \\ %{}) when is_map(params) or is_list(params) do 15 | do_get(@path, params) 16 | end 17 | 18 | @doc """ 19 | Get a connection 20 | 21 | iex> Auth0Ex.Management.Connection.get("some_conn_id") 22 | iex> Auth0Ex.Management.Connection.get("some_conn_id", [fields: "id,name"]) 23 | """ 24 | def get(id) when is_binary(id), do: get(id, []) 25 | 26 | def get(id, params) when is_binary(id) and (is_map(params) or is_list(params)) do 27 | do_get("#{@path}/#{id}", params) 28 | end 29 | 30 | @doc """ 31 | Creates a new connection 32 | 33 | iex> Auth0Ex.Management.Connection.create(%{name: "some_name", strategy: "email"}) 34 | """ 35 | def create(body) do 36 | do_post("#{@path}", body) 37 | end 38 | 39 | @doc """ 40 | Update a connection 41 | 42 | iex> Auth0Ex.Management.Connection.update("some_conn_id", %{name: "new name"}) 43 | """ 44 | def update(id, body) do 45 | do_patch("#{@path}/#{id}", body) 46 | end 47 | 48 | @doc """ 49 | Delete a connection 50 | 51 | iex> Auth0Ex.Management.Connection.delete("some_conn_id") 52 | """ 53 | def delete(id) do 54 | do_delete("#{@path}/#{id}") 55 | end 56 | 57 | @doc """ 58 | Delete a specified connection user by its email 59 | 60 | iex> Auth0Ex.Management.Connection.delete_conn_user("some_conn_id", "samar@example.com") 61 | """ 62 | def delete_conn_user(id, email) do 63 | do_delete("#{@path}/#{id}/users", %{email: email}) 64 | end 65 | end 66 | -------------------------------------------------------------------------------- /lib/auth0_ex/management/device_credential.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Management.DeviceCredential do 2 | @moduledoc """ 3 | A module representing device credential resource on Auth0 4 | """ 5 | use Auth0Ex.Api, for: :mgmt 6 | @path "device-credentials" 7 | 8 | @doc """ 9 | Gets all the device credentials 10 | 11 | iex> Auth0Ex.Management.DeviceCredential.all() 12 | iex> Auth0Ex.Management.DeviceCredential.all(fields: "id,device_name") 13 | """ 14 | def all(params \\ %{}) when is_map(params) or is_list(params) do 15 | do_get(@path, params) 16 | end 17 | 18 | @doc """ 19 | Creates a device public key 20 | 21 | iex> Auth0Ex.Management.DeviceCredential.create(%{device_name: "adev"}) 22 | """ 23 | def create(body) do 24 | do_post(@path, body) 25 | end 26 | 27 | @doc """ 28 | Deletes a device credential 29 | 30 | iex> Auth0Ex.Management.DeviceCredential.delete("device_credential") 31 | """ 32 | def delete(id) do 33 | do_delete("#{@path}/#{id}") 34 | end 35 | end 36 | -------------------------------------------------------------------------------- /lib/auth0_ex/management/email_provider.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Management.EmailProvider do 2 | @moduledoc """ 3 | A module representing emails resource on Auth0 4 | """ 5 | use Auth0Ex.Api, for: :mgmt 6 | @path "emails/provider" 7 | 8 | @doc """ 9 | Gets the email provider 10 | 11 | iex> Auth0Ex.Management.EmailProvider.get() 12 | iex> Auth0Ex.Management.EmailProvider.get(fields: "name,enabled") 13 | """ 14 | def get(params \\ %{}) when is_map(params) or is_list(params) do 15 | do_get(@path, params) 16 | end 17 | 18 | @doc """ 19 | Configures the email provider 20 | 21 | iex> Auth0Ex.Management.EmailProvider.configure(name: "mandrill", enabled: true) 22 | """ 23 | def configure(body), do: do_post(@path, body) 24 | 25 | @doc """ 26 | Updates the email provider 27 | 28 | iex> Auth0Ex.Management.EmailProvider.update(name: "new_name") 29 | """ 30 | def update(body), do: do_patch(@path, body) 31 | 32 | @doc """ 33 | Deletes the email provider 34 | 35 | iex> Auth0Ex.Management.EmailProvider.delete() 36 | """ 37 | def delete, do: do_delete(@path) 38 | end 39 | -------------------------------------------------------------------------------- /lib/auth0_ex/management/job.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Management.Job do 2 | @moduledoc """ 3 | A module representing jobs on Auth0 4 | """ 5 | 6 | use Auth0Ex.Api, for: :mgmt 7 | @path "jobs" 8 | 9 | @doc """ 10 | Sends a verification email for the given user 11 | 12 | iex> Auth0Ex.Management.Job.send_verification_email(%{user_id: "test_user_id, client_id: "test_client_id"}) 13 | """ 14 | def send_verification_email(body \\ %{}) 15 | when is_map(body) do 16 | do_post( 17 | "#{@path}/verification-email", 18 | body 19 | ) 20 | end 21 | end 22 | -------------------------------------------------------------------------------- /lib/auth0_ex/management/log.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Management.Log do 2 | @moduledoc """ 3 | A module represeting log resource on Auth0 4 | """ 5 | use Auth0Ex.Api, for: :mgmt 6 | @path "logs" 7 | 8 | @doc """ 9 | Retrieve the log entries that match the specified criteria 10 | 11 | iex> Auth0Ex.Management.Log.search(page: 1, fields: "user_name") 12 | """ 13 | def search(params) when is_list(params) or is_map(params) do 14 | do_get(@path, params) 15 | end 16 | 17 | @doc """ 18 | Get a log event by id 19 | """ 20 | def get(id) do 21 | do_get("#{@path}/#{id}", %{}) 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /lib/auth0_ex/management/resource_server.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Management.ResourceServer do 2 | @moduledoc """ 3 | A module representing resource servers resource on Auth0 4 | """ 5 | use Auth0Ex.Api, for: :mgmt 6 | @path "resource-servers" 7 | 8 | @doc """ 9 | Gets all the resource servers 10 | 11 | iex> Auth0Ex.Management.ResourceServer.all 12 | """ 13 | def all, do: do_get(@path, %{}) 14 | 15 | @doc """ 16 | Gets a resource server by ID 17 | 18 | iex> Auth0Ex.Management.ResourceServer.get("resserver") 19 | """ 20 | def get(id) do 21 | do_get("#{@path}/#{id}", %{}) 22 | end 23 | 24 | @doc """ 25 | Creates a resource server 26 | 27 | iex> Auth0Ex.Management.ResourceServer.create(%{name: "resserver"}) 28 | """ 29 | def create(body) do 30 | do_post(@path, body) 31 | end 32 | 33 | @doc """ 34 | Updates a resource server 35 | """ 36 | def update(id, body) do 37 | do_patch("#{@path}/#{id}", body) 38 | end 39 | 40 | @doc """ 41 | Deletes a resource server 42 | 43 | iex> Auth0Ex.Management.ResourceServer.delete("resserver") 44 | """ 45 | def delete(id) do 46 | do_delete("#{@path}/#{id}") 47 | end 48 | end 49 | -------------------------------------------------------------------------------- /lib/auth0_ex/management/rule.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Management.Rule do 2 | @moduledoc """ 3 | A module representing rule on Auth0 4 | """ 5 | use Auth0Ex.Api, for: :mgmt 6 | @path "rules" 7 | 8 | @doc """ 9 | Get all rules 10 | 11 | iex> Auth0Ex.Management.Rule.all() 12 | iex> Auth0Ex.Management.Rule.all(enabled: true, fields: "id,name") 13 | """ 14 | def all(params \\ %{}) when is_map(params) or is_list(params) do 15 | do_get(@path, params) 16 | end 17 | 18 | @doc """ 19 | Get a rule 20 | 21 | iex> Auth0Ex.Management.Rule.get("some_rule") 22 | iex> Auth0Ex.Management.Rule.get("some_rule", fields: "id,name") 23 | """ 24 | def get(id) when is_binary(id), do: get(id, []) 25 | 26 | def get(id, params) when is_binary(id) and (is_map(params) or is_list(params)) do 27 | do_get("#{@path}/#{id}", params) 28 | end 29 | 30 | @doc """ 31 | Create a rule 32 | 33 | 34 | iex> Auth0Ex.Management.Rule.create(%{name: "some_rule", script: "some_script_code"}) 35 | """ 36 | def create(body), do: do_post(@path, body) 37 | 38 | @doc """ 39 | Update a rule 40 | 41 | iex> Auth0Ex.Management.Rule.update("some_rule", name: "new_name") 42 | """ 43 | def update(id, body) do 44 | do_patch("#{@path}/#{id}", body) 45 | end 46 | 47 | @doc """ 48 | Deletes a rule 49 | 50 | iex> Auth0Ex.Management.Rule.delete("some_rule") 51 | """ 52 | def delete(id), do: do_delete("#{@path}/#{id}") 53 | end 54 | -------------------------------------------------------------------------------- /lib/auth0_ex/management/ticket.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Management.Ticket do 2 | @moduledoc """ 3 | A module representing tickets on Auth0 4 | """ 5 | 6 | use Auth0Ex.Api, for: :mgmt 7 | @path "tickets" 8 | 9 | @doc """ 10 | Create an email verification ticket 11 | 12 | iex> Auth0Ex.Management.Ticket.email_verification("auth0|user_id", %{result_url: "http://myapp.com/callback", ttl_sec: 0}) 13 | """ 14 | def email_verification(user_id, body) do 15 | do_post("#{@path}/email-verification", Map.put(body, :user_id, user_id)) 16 | end 17 | 18 | @doc """ 19 | Create a password change ticket 20 | 21 | iex> Auth0Ex.Management.Ticket.password_change(%{result_url: "http://myapp.com/callback"}) 22 | """ 23 | def password_change(body) do 24 | do_post("#{@path}/password-change", body) 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /lib/auth0_ex/management/user.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Management.User do 2 | @moduledoc """ 3 | A module representing users on Auth0 4 | """ 5 | 6 | use Auth0Ex.Api, for: :mgmt 7 | @path "users" 8 | 9 | @doc """ 10 | Gets all the users 11 | 12 | iex> Auth0Ex.Management.User.all() 13 | iex> Auth0Ex.Management.User.all(fields: "name", q: "app_metadata.admin:'true'") 14 | """ 15 | def all(params \\ %{}) when is_map(params) or is_list(params) do 16 | do_get(@path, params) 17 | end 18 | 19 | @doc """ 20 | Gets a specific user record with a given user id 21 | 22 | iex> Auth0Ex.Management.User.get("auth0|some_user_id") 23 | iex> Auth0Ex.Management.User.get("auth0|some_user_id", %{fields: "email,name,username"}) 24 | """ 25 | def get(user_id, params \\ %{}) when is_binary(user_id) and is_map(params) do 26 | do_get("#{@path}/#{user_id}", params) 27 | end 28 | 29 | @doc """ 30 | Creates a user for the specified connection 31 | 32 | iex> Auth0Ex.Management.User.create("test_connection", %{email: "test.user@email.com", username: "test_user_name"}) 33 | """ 34 | def create(connection, body \\ %{}) when is_binary(connection) and is_map(body) do 35 | do_post(@path, Map.put(body, :connection, connection)) 36 | end 37 | 38 | @doc """ 39 | Updates a user's information given a user id 40 | 41 | iex> Auth0Ex.Management.User.update("auth0|some_user_id", %{app_metadata: %{admin: false}}) 42 | """ 43 | def update(user_id, body \\ %{}) when is_binary(user_id) and is_map(body) do 44 | do_patch("#{@path}/#{user_id}", body) 45 | end 46 | 47 | @doc """ 48 | Deletes a user with a give user id 49 | 50 | iex> Auth0Ex.Management.User.delete("auth0|some_user_id") 51 | """ 52 | def delete(user_id) when is_binary(user_id) do 53 | do_delete("#{@path}/#{user_id}") 54 | end 55 | 56 | @doc """ 57 | Get a user's log 58 | 59 | iex> Auth0Ex.Management.User.log("auth0|233423") 60 | iex> Auth0Ex.Management.User.log("auth0|23423", page: 2, per_page: 10) 61 | """ 62 | def log(user_id, params) do 63 | do_get("#{@path}/#{user_id}/logs", params) 64 | end 65 | 66 | @doc """ 67 | Get all Guardain enrollments for given user_id 68 | 69 | iex> Auth0Ex.Management.User.enrollments("auth0|234") 70 | """ 71 | def enrollments(user_id), do: do_get("#{@path}/#{user_id}/enrollments", %{}) 72 | 73 | @doc """ 74 | Deletes a user's multifactor provider 75 | 76 | iex> Auth0Ex.Management.User.delete_mfprovider("auth0|23423", "duo") 77 | """ 78 | def delete_mfprovider(user_id, provider) do 79 | do_delete("#{@path}/#{user_id}/multifactor/#{provider}") 80 | end 81 | 82 | @doc """ 83 | Unlinks an identity from the target user, and it becomes a separated user again. 84 | 85 | iex> Auth0Ex.Management.User.unlink("some_user_id", "github", "23984234") 86 | """ 87 | def unlink(primary_id, provider, secondary_id) do 88 | do_delete("#{@path}/#{primary_id}/identities/#{provider}/#{secondary_id}") 89 | end 90 | 91 | @doc """ 92 | Removes current Guardain recovery code and generates new one 93 | 94 | iex> Auth0Ex.Management.User.regenerate_recovery_code("auth0|34234") 95 | """ 96 | def regenerate_recovery_code(id) do 97 | do_post("#{@path}/#{id}/recovery-code-regeneration") 98 | end 99 | 100 | @doc """ 101 | Links the account specified in the body to the given user_id param 102 | 103 | iex> Auth0Ex.Management.User.link("some_user_id", link_with: "secondary_acc_jwt") 104 | iex> Auth0Ex.Management.User.link("some_user_id", provider: "github", user_id: "23423", connection_id: "som") 105 | """ 106 | def link(id, body) do 107 | do_post("#{@path}/#{id}/identities", body) 108 | end 109 | 110 | @doc """ 111 | List the roles associated with a user. Scopes: read:users read:roles 112 | 113 | iex > Auth0Ex.Management.User.roles("auth0|23423") 114 | """ 115 | def roles(user_id, params \\ %{}) do 116 | do_get("#{@path}/#{user_id}/roles", params) 117 | end 118 | 119 | @doc """ 120 | Deletes all users MFA authentication methods. Scopes: delete:authentication_methods 121 | 122 | iex > Auth0Ex.Management.User.delete_authentication_methods("auth0|23423") 123 | """ 124 | def delete_authentication_methods(user_id) do 125 | do_delete("#{@path}/#{user_id}/authentication-methods") 126 | end 127 | 128 | @doc false 129 | defp default_params do 130 | case Application.get_env(:auth0_ex, :v2_search) do 131 | true -> 132 | %{} 133 | 134 | _ -> 135 | %{search_engine: "v3"} 136 | end 137 | end 138 | end 139 | -------------------------------------------------------------------------------- /lib/auth0_ex/management/user_block.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Management.UserBlock do 2 | @moduledoc """ 3 | A module representing user blocks resource on Auth0 4 | """ 5 | use Auth0Ex.Api, for: :mgmt 6 | @path "user-blocks" 7 | 8 | @doc """ 9 | Gets all user blocks for given identifier. 10 | Identifier should be any of: username, phone_number, email 11 | 12 | iex> Auth0Ex.Management.UserBlock.get("some@example.com") 13 | """ 14 | def get(identifier), do: do_get(@path, %{identifier: identifier}) 15 | 16 | @doc """ 17 | Get a user's block 18 | 19 | iex> Auth0Ex.Management.UserBlock.get_user_block("auth0|234234") 20 | """ 21 | def get_user_block(user_id), do: do_get("#{@path}/#{user_id}", %{}) 22 | 23 | @doc """ 24 | Unblock a block by identifier 25 | 26 | iex> Auth0Ex.Management.UserBlock.unblock("some@example.com") 27 | """ 28 | def unblock(identifier), do: do_delete(@path, identifier: identifier) 29 | 30 | @doc """ 31 | Unblock a user 32 | 33 | iex> Auth0Ex.Management.UserBlock.unblock_user("auth0|2342") 34 | """ 35 | def unblock_user(user_id), do: do_delete("#{@path}/#{user_id}") 36 | end 37 | -------------------------------------------------------------------------------- /lib/auth0_ex/parser.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Parser do 2 | @moduledoc """ 3 | Parser to parse all the responses from Auth0 API endpoints 4 | """ 5 | 6 | @type status_code :: integer 7 | @type response :: 8 | {:ok, [struct]} | {:ok, struct} | :ok | {:error, map, status_code} | {:error, map} | any 9 | 10 | @doc """ 11 | Parses the response from auth0 api calls 12 | """ 13 | @spec parse(tuple) :: response 14 | def parse(response) do 15 | case response do 16 | {:ok, %HTTPoison.Response{body: body, headers: headers, status_code: status}} 17 | when status in [200, 201] -> 18 | parse_json_or_html(headers, body) 19 | 20 | {:ok, %HTTPoison.Response{body: _, headers: _, status_code: 204}} -> 21 | :ok 22 | 23 | {:ok, %HTTPoison.Response{body: body, headers: _, status_code: status}} -> 24 | {:error, body, status} 25 | 26 | {:error, %HTTPoison.Error{id: _, reason: reason}} -> 27 | {:error, %{reason: reason}} 28 | 29 | _ -> 30 | response 31 | end 32 | end 33 | 34 | defp parse_json_or_html(headers, body) do 35 | case json_content_type?(headers) do 36 | true -> {:ok, Jason.decode!(body)} 37 | false -> {:ok, body} 38 | end 39 | end 40 | 41 | defp json_content_type?(headers) do 42 | get_media_type(headers) == "application/json" 43 | end 44 | 45 | defp get_media_type(headers) do 46 | Enum.find_value(headers, "application/octet-stream", fn 47 | {header, media_type} when header in ["Content-Type", "content-type"] -> 48 | media_type 49 | |> String.split(";", parts: 2) 50 | |> hd() 51 | 52 | _ -> 53 | false 54 | end) 55 | end 56 | end 57 | -------------------------------------------------------------------------------- /lib/auth0_ex/token_state.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.TokenState do 2 | @moduledoc """ 3 | A simple Agent implementation to store the management token 4 | and potentially other stuff in future for management API 5 | """ 6 | 7 | use Agent 8 | 9 | @doc false 10 | def start_link(_initial_value) do 11 | Agent.start_link(fn -> %{} end, name: __MODULE__) 12 | end 13 | 14 | @doc false 15 | def get(key) do 16 | Agent.get(__MODULE__, &Map.get(&1, key)) 17 | end 18 | 19 | @doc false 20 | def put(key, value) do 21 | Agent.update(__MODULE__, &Map.put(&1, key, value)) 22 | end 23 | 24 | @doc false 25 | def get_all do 26 | Agent.get(__MODULE__, & &1) 27 | end 28 | end 29 | -------------------------------------------------------------------------------- /lib/auth0_ex/utils.ex: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Utils do 2 | @moduledoc """ 3 | Collection module of various utils needed for Auth0Ex 4 | """ 5 | alias Auth0Ex.Authentication.Token 6 | alias Auth0Ex.Config 7 | alias Auth0Ex.TokenState 8 | 9 | def base_url do 10 | "https://#{Config.domain()}/" 11 | end 12 | 13 | def base_url(:mgmt), do: "#{base_url()}api/v2/" 14 | def base_url(_), do: base_url() 15 | def oauth_url, do: "#{base_url()}oauth/token" 16 | 17 | def mgmt_token do 18 | case Config.mgmt_token() do 19 | token when is_binary(token) -> 20 | token 21 | 22 | _ -> 23 | get_token_from_client() 24 | end 25 | end 26 | 27 | def http_opts, do: Config.http_opts() 28 | def ua, do: Config.user_agent() 29 | 30 | def req_header, do: [{"User-Agent", ua()}, {"Content-Type", "application/json"}] 31 | def req_header(:mgmt), do: [{"Authorization", "Bearer #{mgmt_token()}"}] ++ req_header() 32 | def req_header(_), do: req_header() 33 | 34 | defp get_token_from_client do 35 | case TokenState.get(:mgmt_token) do 36 | token when is_binary(token) -> 37 | exp = TokenState.get(:exp) 38 | 39 | if expired?(exp) do 40 | fetch_mgmt_token() 41 | else 42 | token 43 | end 44 | 45 | _ -> 46 | fetch_mgmt_token() 47 | end 48 | end 49 | 50 | defp fetch_mgmt_token do 51 | client_id = Config.mgmt_client_id() 52 | client_secret = Config.mgmt_client_secret() 53 | 54 | {:ok, %{"access_token" => token}} = 55 | Token.client_credentials(client_id, client_secret, base_url(:mgmt)) 56 | 57 | TokenState.put(:mgmt_token, token) 58 | TokenState.put(:exp, exp_from_token(token)) 59 | token 60 | end 61 | 62 | defp exp_from_token(token) do 63 | token 64 | |> String.split(".") 65 | |> Enum.at(1) 66 | |> Base.url_decode64!(padding: false) 67 | |> Jason.decode!() 68 | |> Map.get("exp") 69 | end 70 | 71 | defp expired?(exp), do: exp <= DateTime.utc_now() |> DateTime.to_unix() 72 | end 73 | -------------------------------------------------------------------------------- /mix.exs: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Mixfile do 2 | use Mix.Project 3 | 4 | @source_url "https://github.com/techgaun/auth0_ex" 5 | @version "0.9.0" 6 | 7 | def project do 8 | [ 9 | app: :auth0_ex, 10 | version: @version, 11 | elixir: "~> 1.7", 12 | build_embedded: Mix.env() == :prod, 13 | start_permanent: Mix.env() == :prod, 14 | preferred_cli_env: [ 15 | vcr: :test, 16 | "vcr.delete": :test, 17 | "vcr.check": :test, 18 | "vcr.show": :test 19 | ], 20 | deps: deps(), 21 | package: package(), 22 | description: "An elixir client library for Auth0", 23 | source_url: @source_url, 24 | docs: docs() 25 | ] 26 | end 27 | 28 | def application do 29 | [ 30 | mod: {Auth0Ex.Application, []} 31 | ] 32 | end 33 | 34 | defp deps do 35 | [ 36 | {:httpoison, "~> 2.2"}, 37 | {:jason, "~> 1.4"}, 38 | {:credo, "~> 1.6", only: [:dev, :test]}, 39 | {:exvcr, "~> 0.15", only: :test}, 40 | {:ex_doc, "~> 0.31", only: [:dev]} 41 | ] 42 | end 43 | 44 | defp package do 45 | [ 46 | maintainers: [ 47 | "Samar Acharya" 48 | ], 49 | licenses: ["Apache-2.0"], 50 | links: %{"GitHub" => @source_url} 51 | ] 52 | end 53 | 54 | defp docs do 55 | [ 56 | extras: ["README.md"], 57 | source_ref: "v#{@version}", 58 | source_url: @source_url 59 | ] 60 | end 61 | end 62 | -------------------------------------------------------------------------------- /mix.lock: -------------------------------------------------------------------------------- 1 | %{ 2 | "bunt": {:hex, :bunt, "0.2.0", "951c6e801e8b1d2cbe58ebbd3e616a869061ddadcc4863d0a2182541acae9a38", [:mix], [], "hexpm", "7af5c7e09fe1d40f76c8e4f9dd2be7cebd83909f31fee7cd0e9eadc567da8353"}, 3 | "certifi": {:hex, :certifi, "2.12.0", "2d1cca2ec95f59643862af91f001478c9863c2ac9cb6e2f89780bfd8de987329", [:rebar3], [], "hexpm", "ee68d85df22e554040cdb4be100f33873ac6051387baf6a8f6ce82272340ff1c"}, 4 | "credo": {:hex, :credo, "1.6.4", "ddd474afb6e8c240313f3a7b0d025cc3213f0d171879429bf8535d7021d9ad78", [:mix], [{:bunt, "~> 0.2.0", [hex: :bunt, repo: "hexpm", optional: false]}, {:file_system, "~> 0.2.8", [hex: :file_system, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: false]}], "hexpm", "c28f910b61e1ff829bffa056ef7293a8db50e87f2c57a9b5c3f57eee124536b7"}, 5 | "earmark_parser": {:hex, :earmark_parser, "1.4.39", "424642f8335b05bb9eb611aa1564c148a8ee35c9c8a8bba6e129d51a3e3c6769", [:mix], [], "hexpm", "06553a88d1f1846da9ef066b87b57c6f605552cfbe40d20bd8d59cc6bde41944"}, 6 | "ex_doc": {:hex, :ex_doc, "0.31.1", "8a2355ac42b1cc7b2379da9e40243f2670143721dd50748bf6c3b1184dae2089", [:mix], [{:earmark_parser, "~> 1.4.39", [hex: :earmark_parser, repo: "hexpm", optional: false]}, {:makeup_c, ">= 0.1.1", [hex: :makeup_c, repo: "hexpm", optional: true]}, {:makeup_elixir, "~> 0.14", [hex: :makeup_elixir, repo: "hexpm", optional: false]}, {:makeup_erlang, "~> 0.1", [hex: :makeup_erlang, repo: "hexpm", optional: false]}], "hexpm", "3178c3a407c557d8343479e1ff117a96fd31bafe52a039079593fb0524ef61b0"}, 7 | "exactor": {:hex, :exactor, "2.2.4", "5efb4ddeb2c48d9a1d7c9b465a6fffdd82300eb9618ece5d34c3334d5d7245b1", [:mix], [], "hexpm", "1222419f706e01bfa1095aec9acf6421367dcfab798a6f67c54cf784733cd6b5"}, 8 | "exjsx": {:hex, :exjsx, "4.0.0", "60548841e0212df401e38e63c0078ec57b33e7ea49b032c796ccad8cde794b5c", [:mix], [{:jsx, "~> 2.8.0", [hex: :jsx, repo: "hexpm", optional: false]}], "hexpm", "32e95820a97cffea67830e91514a2ad53b888850442d6d395f53a1ac60c82e07"}, 9 | "exvcr": {:hex, :exvcr, "0.15.1", "772db4d065f5136c6a984c302799a79e4ade3e52701c95425fa2229dd6426886", [:mix], [{:exactor, "~> 2.2", [hex: :exactor, repo: "hexpm", optional: false]}, {:exjsx, "~> 4.0", [hex: :exjsx, repo: "hexpm", optional: false]}, {:finch, "~> 0.16", [hex: :finch, repo: "hexpm", optional: true]}, {:httpoison, "~> 1.0 or ~> 2.0", [hex: :httpoison, repo: "hexpm", optional: true]}, {:httpotion, "~> 3.1", [hex: :httpotion, repo: "hexpm", optional: true]}, {:ibrowse, "4.4.0", [hex: :ibrowse, repo: "hexpm", optional: true]}, {:meck, "~> 0.8", [hex: :meck, repo: "hexpm", optional: false]}], "hexpm", "de4fc18b1d672d9b72bc7468735e19779aa50ea963a1f859ef82cd9e294b13e3"}, 10 | "file_system": {:hex, :file_system, "0.2.10", "fb082005a9cd1711c05b5248710f8826b02d7d1784e7c3451f9c1231d4fc162d", [:mix], [], "hexpm", "41195edbfb562a593726eda3b3e8b103a309b733ad25f3d642ba49696bf715dc"}, 11 | "hackney": {:hex, :hackney, "1.20.1", "8d97aec62ddddd757d128bfd1df6c5861093419f8f7a4223823537bad5d064e2", [:rebar3], [{:certifi, "~> 2.12.0", [hex: :certifi, repo: "hexpm", optional: false]}, {:idna, "~> 6.1.0", [hex: :idna, repo: "hexpm", optional: false]}, {:metrics, "~> 1.0.0", [hex: :metrics, repo: "hexpm", optional: false]}, {:mimerl, "~> 1.1", [hex: :mimerl, repo: "hexpm", optional: false]}, {:parse_trans, "3.4.1", [hex: :parse_trans, repo: "hexpm", optional: false]}, {:ssl_verify_fun, "~> 1.1.0", [hex: :ssl_verify_fun, repo: "hexpm", optional: false]}, {:unicode_util_compat, "~> 0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "fe9094e5f1a2a2c0a7d10918fee36bfec0ec2a979994cff8cfe8058cd9af38e3"}, 12 | "httpoison": {:hex, :httpoison, "2.2.1", "87b7ed6d95db0389f7df02779644171d7319d319178f6680438167d7b69b1f3d", [:mix], [{:hackney, "~> 1.17", [hex: :hackney, repo: "hexpm", optional: false]}], "hexpm", "51364e6d2f429d80e14fe4b5f8e39719cacd03eb3f9a9286e61e216feac2d2df"}, 13 | "idna": {:hex, :idna, "6.1.1", "8a63070e9f7d0c62eb9d9fcb360a7de382448200fbbd1b106cc96d3d8099df8d", [:rebar3], [{:unicode_util_compat, "~>0.7.0", [hex: :unicode_util_compat, repo: "hexpm", optional: false]}], "hexpm", "92376eb7894412ed19ac475e4a86f7b413c1b9fbb5bd16dccd57934157944cea"}, 14 | "jason": {:hex, :jason, "1.4.1", "af1504e35f629ddcdd6addb3513c3853991f694921b1b9368b0bd32beb9f1b63", [:mix], [{:decimal, "~> 1.0 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: true]}], "hexpm", "fbb01ecdfd565b56261302f7e1fcc27c4fb8f32d56eab74db621fc154604a7a1"}, 15 | "jsx": {:hex, :jsx, "2.8.3", "a05252d381885240744d955fbe3cf810504eb2567164824e19303ea59eef62cf", [:mix, :rebar3], [], "hexpm", "fc3499fed7a726995aa659143a248534adc754ebd16ccd437cd93b649a95091f"}, 16 | "makeup": {:hex, :makeup, "1.1.1", "fa0bc768698053b2b3869fa8a62616501ff9d11a562f3ce39580d60860c3a55e", [:mix], [{:nimble_parsec, "~> 1.2.2 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "5dc62fbdd0de44de194898b6710692490be74baa02d9d108bc29f007783b0b48"}, 17 | "makeup_elixir": {:hex, :makeup_elixir, "0.16.1", "cc9e3ca312f1cfeccc572b37a09980287e243648108384b97ff2b76e505c3555", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}, {:nimble_parsec, "~> 1.2.3 or ~> 1.3", [hex: :nimble_parsec, repo: "hexpm", optional: false]}], "hexpm", "e127a341ad1b209bd80f7bd1620a15693a9908ed780c3b763bccf7d200c767c6"}, 18 | "makeup_erlang": {:hex, :makeup_erlang, "0.1.5", "e0ff5a7c708dda34311f7522a8758e23bfcd7d8d8068dc312b5eb41c6fd76eba", [:mix], [{:makeup, "~> 1.0", [hex: :makeup, repo: "hexpm", optional: false]}], "hexpm", "94d2e986428585a21516d7d7149781480013c56e30c6a233534bedf38867a59a"}, 19 | "meck": {:hex, :meck, "0.9.2", "85ccbab053f1db86c7ca240e9fc718170ee5bda03810a6292b5306bf31bae5f5", [:rebar3], [], "hexpm", "81344f561357dc40a8344afa53767c32669153355b626ea9fcbc8da6b3045826"}, 20 | "metrics": {:hex, :metrics, "1.0.1", "25f094dea2cda98213cecc3aeff09e940299d950904393b2a29d191c346a8486", [:rebar3], [], "hexpm", "69b09adddc4f74a40716ae54d140f93beb0fb8978d8636eaded0c31b6f099f16"}, 21 | "mimerl": {:hex, :mimerl, "1.2.0", "67e2d3f571088d5cfd3e550c383094b47159f3eee8ffa08e64106cdf5e981be3", [:rebar3], [], "hexpm", "f278585650aa581986264638ebf698f8bb19df297f66ad91b18910dfc6e19323"}, 22 | "nimble_parsec": {:hex, :nimble_parsec, "1.4.0", "51f9b613ea62cfa97b25ccc2c1b4216e81df970acd8e16e8d1bdc58fef21370d", [:mix], [], "hexpm", "9c565862810fb383e9838c1dd2d7d2c437b3d13b267414ba6af33e50d2d1cf28"}, 23 | "parse_trans": {:hex, :parse_trans, "3.4.1", "6e6aa8167cb44cc8f39441d05193be6e6f4e7c2946cb2759f015f8c56b76e5ff", [:rebar3], [], "hexpm", "620a406ce75dada827b82e453c19cf06776be266f5a67cff34e1ef2cbb60e49a"}, 24 | "ssl_verify_fun": {:hex, :ssl_verify_fun, "1.1.7", "354c321cf377240c7b8716899e182ce4890c5938111a1296add3ec74cf1715df", [:make, :mix, :rebar3], [], "hexpm", "fe4c190e8f37401d30167c8c405eda19469f34577987c76dde613e838bbc67f8"}, 25 | "unicode_util_compat": {:hex, :unicode_util_compat, "0.7.0", "bc84380c9ab48177092f43ac89e4dfa2c6d62b40b8bd132b1059ecc7232f9a78", [:rebar3], [], "hexpm", "25eee6d67df61960cf6a794239566599b09e17e668d3700247bc498638152521"}, 26 | } 27 | -------------------------------------------------------------------------------- /test/lib/auth0_ex/config_test.exs: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.ConfigTest do 2 | use ExUnit.Case 3 | 4 | alias Auth0Ex.Config 5 | 6 | describe "domain/0" do 7 | setup do 8 | original = Application.get_env(:auth0_ex, :domain) 9 | 10 | on_exit(fn -> 11 | Application.put_env(:auth0_ex, :domain, original) 12 | Application.put_env(:auth0_ex, :custom_domain, false) 13 | end) 14 | 15 | :ok 16 | end 17 | 18 | test "domain is returned" do 19 | assert "brighterlink.auth0.com" = Config.domain() 20 | end 21 | 22 | test "custom domain is specified" do 23 | Application.put_env(:auth0_ex, :custom_domain, true) 24 | Application.put_env(:auth0_ex, :domain, "auth.example.com") 25 | 26 | assert "auth.example.com" == Config.domain() 27 | end 28 | 29 | test "deprecated domain is specified" do 30 | Application.put_env(:auth0_ex, :domain, "invalid") 31 | 32 | assert_raise( 33 | ArgumentError, 34 | ~r/the domain specified must be a fully qualified domain name/, 35 | fn -> 36 | Config.domain() 37 | end 38 | ) 39 | end 40 | end 41 | 42 | describe "mgmt_client_id/0" do 43 | setup do 44 | original = Application.get_env(:auth0_ex, :mgmt_client_id) 45 | on_exit(fn -> Application.put_env(:auth0_ex, :mgmt_client_id, original) end) 46 | :ok 47 | end 48 | 49 | test "returns the set value" do 50 | Application.put_env(:auth0_ex, :mgmt_client_id, "foo") 51 | assert "foo" == Config.mgmt_client_id() 52 | end 53 | 54 | test "returns nil if not set" do 55 | Application.delete_env(:auth0_ex, :mgmt_client_id) 56 | assert nil == Config.mgmt_client_id() 57 | end 58 | end 59 | 60 | describe "mgmt_client_secret/0" do 61 | setup do 62 | original = Application.get_env(:auth0_ex, :mgmt_client_secret) 63 | on_exit(fn -> Application.put_env(:auth0_ex, :mgmt_client_secret, original) end) 64 | :ok 65 | end 66 | 67 | test "returns the set value" do 68 | Application.put_env(:auth0_ex, :mgmt_client_secret, "foo") 69 | assert "foo" == Config.mgmt_client_secret() 70 | end 71 | 72 | test "returns nil if not set" do 73 | Application.delete_env(:auth0_ex, :mgmt_client_secret) 74 | assert nil == Config.mgmt_client_secret() 75 | end 76 | end 77 | 78 | describe "mgmt_token/0" do 79 | setup do 80 | original = Application.get_env(:auth0_ex, :mgmt_token) 81 | on_exit(fn -> Application.put_env(:auth0_ex, :mgmt_token, original) end) 82 | :ok 83 | end 84 | 85 | test "returns the set value" do 86 | Application.put_env(:auth0_ex, :mgmt_token, "foo") 87 | assert "foo" == Config.mgmt_token() 88 | end 89 | 90 | test "returns nil if not set" do 91 | Application.delete_env(:auth0_ex, :mgmt_token) 92 | assert nil == Config.mgmt_token() 93 | end 94 | end 95 | 96 | describe "http_opts/0" do 97 | setup do 98 | original = Application.get_env(:auth0_ex, :http_opts) 99 | on_exit(fn -> Application.put_env(:auth0_ex, :http_opts, original) end) 100 | :ok 101 | end 102 | 103 | test "returns the set value" do 104 | Application.put_env(:auth0_ex, :http_opts, foo: :bar) 105 | assert [foo: :bar] == Config.http_opts() 106 | end 107 | 108 | test "returns an empty list if not set" do 109 | Application.delete_env(:auth0_ex, :http_opts) 110 | assert [] == Config.http_opts() 111 | end 112 | end 113 | 114 | describe "user_agent/0" do 115 | setup do 116 | original = Application.get_env(:auth0_ex, :user_agent) 117 | on_exit(fn -> Application.put_env(:auth0_ex, :user_agent, original) end) 118 | :ok 119 | end 120 | 121 | test "returns the set value" do 122 | Application.put_env(:auth0_ex, :user_agent, "MyCustomAgent ") 123 | assert "MyCustomAgent " == Config.user_agent() 124 | end 125 | 126 | test "returns an empty list if not set" do 127 | Application.delete_env(:auth0_ex, :user_agent) 128 | assert "Auth0Ex " == Config.user_agent() 129 | end 130 | end 131 | end 132 | -------------------------------------------------------------------------------- /test/lib/auth0_ex/management/client_test.exs: -------------------------------------------------------------------------------- 1 | defmodule Auth0Ex.Management.ClientTest do 2 | use ExUnit.Case 3 | use ExVCR.Mock, adapter: ExVCR.Adapter.Hackney 4 | alias Auth0Ex.Management.Client 5 | 6 | setup_all do 7 | HTTPoison.start() 8 | end 9 | 10 | test "get all client" do 11 | use_cassette "client_all" do 12 | {:ok, all_clients} = Client.all(%{fields: "name"}) 13 | assert is_list(all_clients) 14 | [client | _] = all_clients 15 | assert map_size(client) === 1 16 | end 17 | end 18 | end 19 | -------------------------------------------------------------------------------- /test/test_helper.exs: -------------------------------------------------------------------------------- 1 | ExUnit.start() 2 | --------------------------------------------------------------------------------