diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..be3f7b2
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,661 @@
+ GNU AFFERO GENERAL PUBLIC LICENSE
+ Version 3, 19 November 2007
+
+ Copyright (C) 2007 Free Software Foundation, Inc.
+ Everyone is permitted to copy and distribute verbatim copies
+ of this license document, but changing it is not allowed.
+
+ Preamble
+
+ The GNU Affero General Public License is a free, copyleft license for
+software and other kinds of works, specifically designed to ensure
+cooperation with the community in the case of network server software.
+
+ The licenses for most software and other practical works are designed
+to take away your freedom to share and change the works. By contrast,
+our General Public Licenses are intended to guarantee your freedom to
+share and change all versions of a program--to make sure it remains free
+software for all its users.
+
+ When we speak of free software, we are referring to freedom, not
+price. Our General Public Licenses are designed to make sure that you
+have the freedom to distribute copies of free software (and charge for
+them if you wish), that you receive source code or can get it if you
+want it, that you can change the software or use pieces of it in new
+free programs, and that you know you can do these things.
+
+ Developers that use our General Public Licenses protect your rights
+with two steps: (1) assert copyright on the software, and (2) offer
+you this License which gives you legal permission to copy, distribute
+and/or modify the software.
+
+ A secondary benefit of defending all users' freedom is that
+improvements made in alternate versions of the program, if they
+receive widespread use, become available for other developers to
+incorporate. Many developers of free software are heartened and
+encouraged by the resulting cooperation. However, in the case of
+software used on network servers, this result may fail to come about.
+The GNU General Public License permits making a modified version and
+letting the public access it on a server without ever releasing its
+source code to the public.
+
+ The GNU Affero General Public License is designed specifically to
+ensure that, in such cases, the modified source code becomes available
+to the community. It requires the operator of a network server to
+provide the source code of the modified version running there to the
+users of that server. Therefore, public use of a modified version, on
+a publicly accessible server, gives the public access to the source
+code of the modified version.
+
+ An older license, called the Affero General Public License and
+published by Affero, was designed to accomplish similar goals. This is
+a different license, not a version of the Affero GPL, but Affero has
+released a new version of the Affero GPL which permits relicensing under
+this license.
+
+ The precise terms and conditions for copying, distribution and
+modification follow.
+
+ TERMS AND CONDITIONS
+
+ 0. Definitions.
+
+ "This License" refers to version 3 of the GNU Affero General Public License.
+
+ "Copyright" also means copyright-like laws that apply to other kinds of
+works, such as semiconductor masks.
+
+ "The Program" refers to any copyrightable work licensed under this
+License. Each licensee is addressed as "you". "Licensees" and
+"recipients" may be individuals or organizations.
+
+ To "modify" a work means to copy from or adapt all or part of the work
+in a fashion requiring copyright permission, other than the making of an
+exact copy. The resulting work is called a "modified version" of the
+earlier work or a work "based on" the earlier work.
+
+ A "covered work" means either the unmodified Program or a work based
+on the Program.
+
+ To "propagate" a work means to do anything with it that, without
+permission, would make you directly or secondarily liable for
+infringement under applicable copyright law, except executing it on a
+computer or modifying a private copy. Propagation includes copying,
+distribution (with or without modification), making available to the
+public, and in some countries other activities as well.
+
+ To "convey" a work means any kind of propagation that enables other
+parties to make or receive copies. Mere interaction with a user through
+a computer network, with no transfer of a copy, is not conveying.
+
+ An interactive user interface displays "Appropriate Legal Notices"
+to the extent that it includes a convenient and prominently visible
+feature that (1) displays an appropriate copyright notice, and (2)
+tells the user that there is no warranty for the work (except to the
+extent that warranties are provided), that licensees may convey the
+work under this License, and how to view a copy of this License. If
+the interface presents a list of user commands or options, such as a
+menu, a prominent item in the list meets this criterion.
+
+ 1. Source Code.
+
+ The "source code" for a work means the preferred form of the work
+for making modifications to it. "Object code" means any non-source
+form of a work.
+
+ A "Standard Interface" means an interface that either is an official
+standard defined by a recognized standards body, or, in the case of
+interfaces specified for a particular programming language, one that
+is widely used among developers working in that language.
+
+ The "System Libraries" of an executable work include anything, other
+than the work as a whole, that (a) is included in the normal form of
+packaging a Major Component, but which is not part of that Major
+Component, and (b) serves only to enable use of the work with that
+Major Component, or to implement a Standard Interface for which an
+implementation is available to the public in source code form. A
+"Major Component", in this context, means a major essential component
+(kernel, window system, and so on) of the specific operating system
+(if any) on which the executable work runs, or a compiler used to
+produce the work, or an object code interpreter used to run it.
+
+ The "Corresponding Source" for a work in object code form means all
+the source code needed to generate, install, and (for an executable
+work) run the object code and to modify the work, including scripts to
+control those activities. However, it does not include the work's
+System Libraries, or general-purpose tools or generally available free
+programs which are used unmodified in performing those activities but
+which are not part of the work. For example, Corresponding Source
+includes interface definition files associated with source files for
+the work, and the source code for shared libraries and dynamically
+linked subprograms that the work is specifically designed to require,
+such as by intimate data communication or control flow between those
+subprograms and other parts of the work.
+
+ The Corresponding Source need not include anything that users
+can regenerate automatically from other parts of the Corresponding
+Source.
+
+ The Corresponding Source for a work in source code form is that
+same work.
+
+ 2. Basic Permissions.
+
+ All rights granted under this License are granted for the term of
+copyright on the Program, and are irrevocable provided the stated
+conditions are met. This License explicitly affirms your unlimited
+permission to run the unmodified Program. The output from running a
+covered work is covered by this License only if the output, given its
+content, constitutes a covered work. This License acknowledges your
+rights of fair use or other equivalent, as provided by copyright law.
+
+ You may make, run and propagate covered works that you do not
+convey, without conditions so long as your license otherwise remains
+in force. You may convey covered works to others for the sole purpose
+of having them make modifications exclusively for you, or provide you
+with facilities for running those works, provided that you comply with
+the terms of this License in conveying all material for which you do
+not control copyright. Those thus making or running the covered works
+for you must do so exclusively on your behalf, under your direction
+and control, on terms that prohibit them from making any copies of
+your copyrighted material outside their relationship with you.
+
+ Conveying under any other circumstances is permitted solely under
+the conditions stated below. Sublicensing is not allowed; section 10
+makes it unnecessary.
+
+ 3. Protecting Users' Legal Rights From Anti-Circumvention Law.
+
+ No covered work shall be deemed part of an effective technological
+measure under any applicable law fulfilling obligations under article
+11 of the WIPO copyright treaty adopted on 20 December 1996, or
+similar laws prohibiting or restricting circumvention of such
+measures.
+
+ When you convey a covered work, you waive any legal power to forbid
+circumvention of technological measures to the extent such circumvention
+is effected by exercising rights under this License with respect to
+the covered work, and you disclaim any intention to limit operation or
+modification of the work as a means of enforcing, against the work's
+users, your or third parties' legal rights to forbid circumvention of
+technological measures.
+
+ 4. Conveying Verbatim Copies.
+
+ You may convey verbatim copies of the Program's source code as you
+receive it, in any medium, provided that you conspicuously and
+appropriately publish on each copy an appropriate copyright notice;
+keep intact all notices stating that this License and any
+non-permissive terms added in accord with section 7 apply to the code;
+keep intact all notices of the absence of any warranty; and give all
+recipients a copy of this License along with the Program.
+
+ You may charge any price or no price for each copy that you convey,
+and you may offer support or warranty protection for a fee.
+
+ 5. Conveying Modified Source Versions.
+
+ You may convey a work based on the Program, or the modifications to
+produce it from the Program, in the form of source code under the
+terms of section 4, provided that you also meet all of these conditions:
+
+ a) The work must carry prominent notices stating that you modified
+ it, and giving a relevant date.
+
+ b) The work must carry prominent notices stating that it is
+ released under this License and any conditions added under section
+ 7. This requirement modifies the requirement in section 4 to
+ "keep intact all notices".
+
+ c) You must license the entire work, as a whole, under this
+ License to anyone who comes into possession of a copy. This
+ License will therefore apply, along with any applicable section 7
+ additional terms, to the whole of the work, and all its parts,
+ regardless of how they are packaged. This License gives no
+ permission to license the work in any other way, but it does not
+ invalidate such permission if you have separately received it.
+
+ d) If the work has interactive user interfaces, each must display
+ Appropriate Legal Notices; however, if the Program has interactive
+ interfaces that do not display Appropriate Legal Notices, your
+ work need not make them do so.
+
+ A compilation of a covered work with other separate and independent
+works, which are not by their nature extensions of the covered work,
+and which are not combined with it such as to form a larger program,
+in or on a volume of a storage or distribution medium, is called an
+"aggregate" if the compilation and its resulting copyright are not
+used to limit the access or legal rights of the compilation's users
+beyond what the individual works permit. Inclusion of a covered work
+in an aggregate does not cause this License to apply to the other
+parts of the aggregate.
+
+ 6. Conveying Non-Source Forms.
+
+ You may convey a covered work in object code form under the terms
+of sections 4 and 5, provided that you also convey the
+machine-readable Corresponding Source under the terms of this License,
+in one of these ways:
+
+ a) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by the
+ Corresponding Source fixed on a durable physical medium
+ customarily used for software interchange.
+
+ b) Convey the object code in, or embodied in, a physical product
+ (including a physical distribution medium), accompanied by a
+ written offer, valid for at least three years and valid for as
+ long as you offer spare parts or customer support for that product
+ model, to give anyone who possesses the object code either (1) a
+ copy of the Corresponding Source for all the software in the
+ product that is covered by this License, on a durable physical
+ medium customarily used for software interchange, for a price no
+ more than your reasonable cost of physically performing this
+ conveying of source, or (2) access to copy the
+ Corresponding Source from a network server at no charge.
+
+ c) Convey individual copies of the object code with a copy of the
+ written offer to provide the Corresponding Source. This
+ alternative is allowed only occasionally and noncommercially, and
+ only if you received the object code with such an offer, in accord
+ with subsection 6b.
+
+ d) Convey the object code by offering access from a designated
+ place (gratis or for a charge), and offer equivalent access to the
+ Corresponding Source in the same way through the same place at no
+ further charge. You need not require recipients to copy the
+ Corresponding Source along with the object code. If the place to
+ copy the object code is a network server, the Corresponding Source
+ may be on a different server (operated by you or a third party)
+ that supports equivalent copying facilities, provided you maintain
+ clear directions next to the object code saying where to find the
+ Corresponding Source. Regardless of what server hosts the
+ Corresponding Source, you remain obligated to ensure that it is
+ available for as long as needed to satisfy these requirements.
+
+ e) Convey the object code using peer-to-peer transmission, provided
+ you inform other peers where the object code and Corresponding
+ Source of the work are being offered to the general public at no
+ charge under subsection 6d.
+
+ A separable portion of the object code, whose source code is excluded
+from the Corresponding Source as a System Library, need not be
+included in conveying the object code work.
+
+ A "User Product" is either (1) a "consumer product", which means any
+tangible personal property which is normally used for personal, family,
+or household purposes, or (2) anything designed or sold for incorporation
+into a dwelling. In determining whether a product is a consumer product,
+doubtful cases shall be resolved in favor of coverage. For a particular
+product received by a particular user, "normally used" refers to a
+typical or common use of that class of product, regardless of the status
+of the particular user or of the way in which the particular user
+actually uses, or expects or is expected to use, the product. A product
+is a consumer product regardless of whether the product has substantial
+commercial, industrial or non-consumer uses, unless such uses represent
+the only significant mode of use of the product.
+
+ "Installation Information" for a User Product means any methods,
+procedures, authorization keys, or other information required to install
+and execute modified versions of a covered work in that User Product from
+a modified version of its Corresponding Source. The information must
+suffice to ensure that the continued functioning of the modified object
+code is in no case prevented or interfered with solely because
+modification has been made.
+
+ If you convey an object code work under this section in, or with, or
+specifically for use in, a User Product, and the conveying occurs as
+part of a transaction in which the right of possession and use of the
+User Product is transferred to the recipient in perpetuity or for a
+fixed term (regardless of how the transaction is characterized), the
+Corresponding Source conveyed under this section must be accompanied
+by the Installation Information. But this requirement does not apply
+if neither you nor any third party retains the ability to install
+modified object code on the User Product (for example, the work has
+been installed in ROM).
+
+ The requirement to provide Installation Information does not include a
+requirement to continue to provide support service, warranty, or updates
+for a work that has been modified or installed by the recipient, or for
+the User Product in which it has been modified or installed. Access to a
+network may be denied when the modification itself materially and
+adversely affects the operation of the network or violates the rules and
+protocols for communication across the network.
+
+ Corresponding Source conveyed, and Installation Information provided,
+in accord with this section must be in a format that is publicly
+documented (and with an implementation available to the public in
+source code form), and must require no special password or key for
+unpacking, reading or copying.
+
+ 7. Additional Terms.
+
+ "Additional permissions" are terms that supplement the terms of this
+License by making exceptions from one or more of its conditions.
+Additional permissions that are applicable to the entire Program shall
+be treated as though they were included in this License, to the extent
+that they are valid under applicable law. If additional permissions
+apply only to part of the Program, that part may be used separately
+under those permissions, but the entire Program remains governed by
+this License without regard to the additional permissions.
+
+ When you convey a copy of a covered work, you may at your option
+remove any additional permissions from that copy, or from any part of
+it. (Additional permissions may be written to require their own
+removal in certain cases when you modify the work.) You may place
+additional permissions on material, added by you to a covered work,
+for which you have or can give appropriate copyright permission.
+
+ Notwithstanding any other provision of this License, for material you
+add to a covered work, you may (if authorized by the copyright holders of
+that material) supplement the terms of this License with terms:
+
+ a) Disclaiming warranty or limiting liability differently from the
+ terms of sections 15 and 16 of this License; or
+
+ b) Requiring preservation of specified reasonable legal notices or
+ author attributions in that material or in the Appropriate Legal
+ Notices displayed by works containing it; or
+
+ c) Prohibiting misrepresentation of the origin of that material, or
+ requiring that modified versions of such material be marked in
+ reasonable ways as different from the original version; or
+
+ d) Limiting the use for publicity purposes of names of licensors or
+ authors of the material; or
+
+ e) Declining to grant rights under trademark law for use of some
+ trade names, trademarks, or service marks; or
+
+ f) Requiring indemnification of licensors and authors of that
+ material by anyone who conveys the material (or modified versions of
+ it) with contractual assumptions of liability to the recipient, for
+ any liability that these contractual assumptions directly impose on
+ those licensors and authors.
+
+ All other non-permissive additional terms are considered "further
+restrictions" within the meaning of section 10. If the Program as you
+received it, or any part of it, contains a notice stating that it is
+governed by this License along with a term that is a further
+restriction, you may remove that term. If a license document contains
+a further restriction but permits relicensing or conveying under this
+License, you may add to a covered work material governed by the terms
+of that license document, provided that the further restriction does
+not survive such relicensing or conveying.
+
+ If you add terms to a covered work in accord with this section, you
+must place, in the relevant source files, a statement of the
+additional terms that apply to those files, or a notice indicating
+where to find the applicable terms.
+
+ Additional terms, permissive or non-permissive, may be stated in the
+form of a separately written license, or stated as exceptions;
+the above requirements apply either way.
+
+ 8. Termination.
+
+ You may not propagate or modify a covered work except as expressly
+provided under this License. Any attempt otherwise to propagate or
+modify it is void, and will automatically terminate your rights under
+this License (including any patent licenses granted under the third
+paragraph of section 11).
+
+ However, if you cease all violation of this License, then your
+license from a particular copyright holder is reinstated (a)
+provisionally, unless and until the copyright holder explicitly and
+finally terminates your license, and (b) permanently, if the copyright
+holder fails to notify you of the violation by some reasonable means
+prior to 60 days after the cessation.
+
+ Moreover, your license from a particular copyright holder is
+reinstated permanently if the copyright holder notifies you of the
+violation by some reasonable means, this is the first time you have
+received notice of violation of this License (for any work) from that
+copyright holder, and you cure the violation prior to 30 days after
+your receipt of the notice.
+
+ Termination of your rights under this section does not terminate the
+licenses of parties who have received copies or rights from you under
+this License. If your rights have been terminated and not permanently
+reinstated, you do not qualify to receive new licenses for the same
+material under section 10.
+
+ 9. Acceptance Not Required for Having Copies.
+
+ You are not required to accept this License in order to receive or
+run a copy of the Program. Ancillary propagation of a covered work
+occurring solely as a consequence of using peer-to-peer transmission
+to receive a copy likewise does not require acceptance. However,
+nothing other than this License grants you permission to propagate or
+modify any covered work. These actions infringe copyright if you do
+not accept this License. Therefore, by modifying or propagating a
+covered work, you indicate your acceptance of this License to do so.
+
+ 10. Automatic Licensing of Downstream Recipients.
+
+ Each time you convey a covered work, the recipient automatically
+receives a license from the original licensors, to run, modify and
+propagate that work, subject to this License. You are not responsible
+for enforcing compliance by third parties with this License.
+
+ An "entity transaction" is a transaction transferring control of an
+organization, or substantially all assets of one, or subdividing an
+organization, or merging organizations. If propagation of a covered
+work results from an entity transaction, each party to that
+transaction who receives a copy of the work also receives whatever
+licenses to the work the party's predecessor in interest had or could
+give under the previous paragraph, plus a right to possession of the
+Corresponding Source of the work from the predecessor in interest, if
+the predecessor has it or can get it with reasonable efforts.
+
+ You may not impose any further restrictions on the exercise of the
+rights granted or affirmed under this License. For example, you may
+not impose a license fee, royalty, or other charge for exercise of
+rights granted under this License, and you may not initiate litigation
+(including a cross-claim or counterclaim in a lawsuit) alleging that
+any patent claim is infringed by making, using, selling, offering for
+sale, or importing the Program or any portion of it.
+
+ 11. Patents.
+
+ A "contributor" is a copyright holder who authorizes use under this
+License of the Program or a work on which the Program is based. The
+work thus licensed is called the contributor's "contributor version".
+
+ A contributor's "essential patent claims" are all patent claims
+owned or controlled by the contributor, whether already acquired or
+hereafter acquired, that would be infringed by some manner, permitted
+by this License, of making, using, or selling its contributor version,
+but do not include claims that would be infringed only as a
+consequence of further modification of the contributor version. For
+purposes of this definition, "control" includes the right to grant
+patent sublicenses in a manner consistent with the requirements of
+this License.
+
+ Each contributor grants you a non-exclusive, worldwide, royalty-free
+patent license under the contributor's essential patent claims, to
+make, use, sell, offer for sale, import and otherwise run, modify and
+propagate the contents of its contributor version.
+
+ In the following three paragraphs, a "patent license" is any express
+agreement or commitment, however denominated, not to enforce a patent
+(such as an express permission to practice a patent or covenant not to
+sue for patent infringement). To "grant" such a patent license to a
+party means to make such an agreement or commitment not to enforce a
+patent against the party.
+
+ If you convey a covered work, knowingly relying on a patent license,
+and the Corresponding Source of the work is not available for anyone
+to copy, free of charge and under the terms of this License, through a
+publicly available network server or other readily accessible means,
+then you must either (1) cause the Corresponding Source to be so
+available, or (2) arrange to deprive yourself of the benefit of the
+patent license for this particular work, or (3) arrange, in a manner
+consistent with the requirements of this License, to extend the patent
+license to downstream recipients. "Knowingly relying" means you have
+actual knowledge that, but for the patent license, your conveying the
+covered work in a country, or your recipient's use of the covered work
+in a country, would infringe one or more identifiable patents in that
+country that you have reason to believe are valid.
+
+ If, pursuant to or in connection with a single transaction or
+arrangement, you convey, or propagate by procuring conveyance of, a
+covered work, and grant a patent license to some of the parties
+receiving the covered work authorizing them to use, propagate, modify
+or convey a specific copy of the covered work, then the patent license
+you grant is automatically extended to all recipients of the covered
+work and works based on it.
+
+ A patent license is "discriminatory" if it does not include within
+the scope of its coverage, prohibits the exercise of, or is
+conditioned on the non-exercise of one or more of the rights that are
+specifically granted under this License. You may not convey a covered
+work if you are a party to an arrangement with a third party that is
+in the business of distributing software, under which you make payment
+to the third party based on the extent of your activity of conveying
+the work, and under which the third party grants, to any of the
+parties who would receive the covered work from you, a discriminatory
+patent license (a) in connection with copies of the covered work
+conveyed by you (or copies made from those copies), or (b) primarily
+for and in connection with specific products or compilations that
+contain the covered work, unless you entered into that arrangement,
+or that patent license was granted, prior to 28 March 2007.
+
+ Nothing in this License shall be construed as excluding or limiting
+any implied license or other defenses to infringement that may
+otherwise be available to you under applicable patent law.
+
+ 12. No Surrender of Others' Freedom.
+
+ If conditions are imposed on you (whether by court order, agreement or
+otherwise) that contradict the conditions of this License, they do not
+excuse you from the conditions of this License. If you cannot convey a
+covered work so as to satisfy simultaneously your obligations under this
+License and any other pertinent obligations, then as a consequence you may
+not convey it at all. For example, if you agree to terms that obligate you
+to collect a royalty for further conveying from those to whom you convey
+the Program, the only way you could satisfy both those terms and this
+License would be to refrain entirely from conveying the Program.
+
+ 13. Remote Network Interaction; Use with the GNU General Public License.
+
+ Notwithstanding any other provision of this License, if you modify the
+Program, your modified version must prominently offer all users
+interacting with it remotely through a computer network (if your version
+supports such interaction) an opportunity to receive the Corresponding
+Source of your version by providing access to the Corresponding Source
+from a network server at no charge, through some standard or customary
+means of facilitating copying of software. This Corresponding Source
+shall include the Corresponding Source for any work covered by version 3
+of the GNU General Public License that is incorporated pursuant to the
+following paragraph.
+
+ Notwithstanding any other provision of this License, you have
+permission to link or combine any covered work with a work licensed
+under version 3 of the GNU General Public License into a single
+combined work, and to convey the resulting work. The terms of this
+License will continue to apply to the part which is the covered work,
+but the work with which it is combined will remain governed by version
+3 of the GNU General Public License.
+
+ 14. Revised Versions of this License.
+
+ The Free Software Foundation may publish revised and/or new versions of
+the GNU Affero General Public License from time to time. Such new versions
+will be similar in spirit to the present version, but may differ in detail to
+address new problems or concerns.
+
+ Each version is given a distinguishing version number. If the
+Program specifies that a certain numbered version of the GNU Affero General
+Public License "or any later version" applies to it, you have the
+option of following the terms and conditions either of that numbered
+version or of any later version published by the Free Software
+Foundation. If the Program does not specify a version number of the
+GNU Affero General Public License, you may choose any version ever published
+by the Free Software Foundation.
+
+ If the Program specifies that a proxy can decide which future
+versions of the GNU Affero General Public License can be used, that proxy's
+public statement of acceptance of a version permanently authorizes you
+to choose that version for the Program.
+
+ Later license versions may give you additional or different
+permissions. However, no additional obligations are imposed on any
+author or copyright holder as a result of your choosing to follow a
+later version.
+
+ 15. Disclaimer of Warranty.
+
+ THERE IS NO WARRANTY FOR THE PROGRAM, TO THE EXTENT PERMITTED BY
+APPLICABLE LAW. EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT
+HOLDERS AND/OR OTHER PARTIES PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY
+OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO,
+THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM
+IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF
+ALL NECESSARY SERVICING, REPAIR OR CORRECTION.
+
+ 16. Limitation of Liability.
+
+ IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
+WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MODIFIES AND/OR CONVEYS
+THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, INCLUDING ANY
+GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THE
+USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED TO LOSS OF
+DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD
+PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER PROGRAMS),
+EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF
+SUCH DAMAGES.
+
+ 17. Interpretation of Sections 15 and 16.
+
+ If the disclaimer of warranty and limitation of liability provided
+above cannot be given local legal effect according to their terms,
+reviewing courts shall apply local law that most closely approximates
+an absolute waiver of all civil liability in connection with the
+Program, unless a warranty or assumption of liability accompanies a
+copy of the Program in return for a fee.
+
+ END OF TERMS AND CONDITIONS
+
+ How to Apply These Terms to Your New Programs
+
+ If you develop a new program, and you want it to be of the greatest
+possible use to the public, the best way to achieve this is to make it
+free software which everyone can redistribute and change under these terms.
+
+ To do so, attach the following notices to the program. It is safest
+to attach them to the start of each source file to most effectively
+state the exclusion of warranty; and each file should have at least
+the "copyright" line and a pointer to where the full notice is found.
+
+
+ Copyright (C)
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see .
+
+Also add information on how to contact you by electronic and paper mail.
+
+ If your software can interact with users remotely through a computer
+network, you should also make sure that it provides a way for users to
+get its source. For example, if your program is a web application, its
+interface could display a "Source" link that leads users to an archive
+of the code. There are many ways you could offer source, and different
+solutions will be better for different programs; see section 13 for the
+specific requirements.
+
+ You should also get your employer (if you work as a programmer) or school,
+if any, to sign a "copyright disclaimer" for the program, if necessary.
+For more information on this, and how to apply and follow the GNU AGPL, see
+.
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..e844969
--- /dev/null
+++ b/README.md
@@ -0,0 +1,112 @@
+# krakenkey-app
+
+[](https://github.com/KrakenKey/app/blob/main/backend/LICENSE)
+
+Web application for [KrakenKey](https://krakenkey.io) — TLS certificate lifecycle management. This repo contains the API backend, web dashboard, and shared type library.
+
+## Architecture
+
+```
+app/
+├── backend/ NestJS API — certificate issuance, domain verification, billing
+├── frontend/ React + Vite dashboard — manage domains, certs, and endpoints
+└── shared/ @krakenkey/shared — TypeScript types and route constants
+```
+
+**Backend** — NestJS 11, TypeORM, PostgreSQL, Redis, Stripe. Handles ACME certificate issuance, domain DNS verification, endpoint TLS monitoring, org/RBAC, and billing. Exposes a REST API with OpenAPI docs.
+
+**Frontend** — React 19, Vite, Tailwind CSS, React Router. Provides the web dashboard for managing domains, certificates, endpoints, API keys, and account settings.
+
+**Shared** — TypeScript-only package consumed by both backend and frontend. Exports domain types (`User`, `Domain`, `TlsCert`, `Endpoint`, `ApiKey`, `Subscription`, etc.) and API route constants.
+
+## Getting started
+
+### Prerequisites
+
+- Node.js 22+
+- Yarn
+- PostgreSQL 18+
+- Redis 8+
+
+### Development (devcontainer)
+
+The recommended setup uses the [devcontainer](https://github.com/KrakenKey/krakenkey/tree/main/.devcontainer) in the [krakenkey](https://github.com/KrakenKey/krakenkey) monorepo, which provisions PostgreSQL, Redis, and Traefik automatically.
+
+### Manual setup
+
+```bash
+# Install dependencies
+cd backend && yarn install && cd ..
+cd frontend && yarn install && cd ..
+cd shared && yarn install && yarn build && cd ..
+
+# Run database migrations
+cd backend && yarn migration:run
+
+# Start the API (port 8080)
+cd backend && yarn start:dev
+
+# Start the dashboard (port 5173)
+cd frontend && yarn dev --host
+```
+
+### Environment variables
+
+Copy the template and fill in your values:
+
+```bash
+cp .env.template .env
+```
+
+Key variables:
+
+| Variable | Description |
+|---|---|
+| `KK_API_PORT` | API listen port (default `8080`) |
+| `KK_DB_HOST` | PostgreSQL host |
+| `KK_DB_NAME` | PostgreSQL database name |
+| `KK_REDIS_URL` | Redis connection URL |
+| `KK_JWT_SECRET` | JWT signing secret |
+| `KK_ACME_EMAIL` | Email for ACME account registration |
+
+See the env template for the full list.
+
+## Scripts
+
+### Backend
+
+```bash
+yarn start:dev # Development with hot reload
+yarn build # Compile TypeScript
+yarn test # Unit tests
+yarn test:e2e # End-to-end tests
+yarn test:cov # Coverage report
+yarn migration:run # Run pending migrations
+yarn migration:revert # Revert last migration
+yarn openapi:export # Export OpenAPI spec
+```
+
+### Frontend
+
+```bash
+yarn dev --host # Dev server with HMR
+yarn build # Production build
+yarn test # Unit tests (Vitest)
+yarn test:e2e # E2E tests (Playwright)
+yarn lint # ESLint
+```
+
+## API documentation
+
+When running locally, Swagger UI is available at `/api/docs` on the backend port.
+
+## Related repos
+
+- [krakenkey/cli](https://github.com/KrakenKey/cli) — CLI for terminal and CI/CD workflows
+- [krakenkey/probe](https://github.com/KrakenKey/probe) — TLS endpoint monitoring agent
+- [krakenkey/web](https://github.com/KrakenKey/web) — Marketing site
+- [krakenkey/krakenkey](https://github.com/KrakenKey/krakenkey) — Devcontainer and workspace orchestration
+
+## License
+
+[AGPL-3.0](LICENSE)
diff --git a/backend/README.md b/backend/README.md
index fbf28c6..3c4c02c 100644
--- a/backend/README.md
+++ b/backend/README.md
@@ -18,18 +18,29 @@ The API runs on port 8080 by default. Swagger docs are available at `/swagger`.
| `auth/` | Authentik OIDC login, JWT validation, API key management |
| `certs/tls/` | Certificate submission, ACME issuance, DNS-01 challenges |
| `domains/` | Domain registration and DNS verification |
-| `users/` | User entities and management |
+| `users/` | User entities and account management |
+| `billing/` | Stripe subscriptions, plan limits, checkout |
+| `organizations/` | Team management with role-based access |
+| `endpoints/` | TLS endpoint monitoring with probe scanning |
+| `health/` | Health check and Kubernetes probes |
+| `metrics/` | Prometheus metrics |
+| `notifications/` | Email notifications |
+| `feedback/` | User feedback collection |
+| `throttler/` | Rate limiting |
## Documentation
See [docs/](docs/README.md) for detailed documentation:
-- [Architecture](docs/ARCHITECTURE.md)
-- [API Reference](docs/API_REFERENCE.md)
-- [Certificate Issuance Flow](docs/CERTIFICATE_FLOW.md)
-- [Database Schema](docs/DATABASE.md)
-- [Configuration](docs/CONFIGURATION.md)
-- [Integrations](docs/INTEGRATIONS.md)
+- [Architecture](docs/ARCHITECTURE.md) — system design and module structure
+- [API Reference](docs/API_REFERENCE.md) — endpoint documentation
+- [Certificate Flow](docs/CERTIFICATE_FLOW.md) — CSR generation, issuance lifecycle, renewal
+- [Database Schema](docs/DATABASE.md) — entities and relationships
+- [Configuration](docs/CONFIGURATION.md) — environment variables reference
+- [Integrations](docs/INTEGRATIONS.md) — Cloudflare, Let's Encrypt, Authentik, Stripe setup
+- [Billing](docs/BILLING.md) — plans, subscriptions, Stripe integration
+- [Organizations](docs/ORGANIZATIONS.md) — teams, roles, resource sharing
+- [Endpoints](docs/ENDPOINTS.md) — TLS endpoint monitoring and probes
## Testing
diff --git a/backend/docs/API_REFERENCE.md b/backend/docs/API_REFERENCE.md
index 8331285..22699a0 100644
--- a/backend/docs/API_REFERENCE.md
+++ b/backend/docs/API_REFERENCE.md
@@ -3,610 +3,591 @@
## Base URL
```
-http://localhost:8888
+http://localhost:8080
```
## Documentation
-Interactive API documentation available at:
+Interactive Swagger docs available at `/swagger` (development mode only).
+
+## Authentication
+
+All protected endpoints require an `Authorization` header:
+
```
-http://localhost:8888/swagger
+Authorization: Bearer
```
-## Endpoints
+**JWT tokens** — Short-lived, obtained via Authentik OIDC login flow.
-### Health Check
+**API keys** — Long-lived, prefixed with `kk_`. Created via the API or dashboard. The raw key is shown only once at creation time.
-#### GET /
-Returns API status and version information.
+Both methods use the same header format. The backend tries JWT validation first, then falls back to API key validation.
+
+## Error Response Format
+
+All errors follow a consistent JSON format:
-**Response**:
```json
{
- "status": "ok",
- "version": "0.1.0"
+ "statusCode": 400,
+ "message": "Invalid CSR PEM format",
+ "error": "Bad Request",
+ "timestamp": "2026-03-27T10:00:00.000Z",
+ "path": "/certs/tls"
}
```
-**Response Code**: 200 OK
+### Common HTTP Status Codes
-**Use Case**: Health checks, readiness probes, liveness checks
+| Code | Meaning |
+|------|---------|
+| 200 | Success (GET, PATCH, DELETE) |
+| 201 | Created (POST) |
+| 400 | Validation failure or invalid input |
+| 401 | Missing or invalid authentication |
+| 403 | Insufficient permissions (wrong role) |
+| 404 | Resource not found |
+| 409 | Conflict (duplicate resource) |
+| 422 | Validation errors (detailed) |
+| 429 | Rate limit exceeded |
+| 500 | Internal server error |
---
-### Certificate Management
+## Health Check
-#### POST /certs/tls
-Submit a Certificate Signing Request (CSR) for certificate issuance.
+### GET /
+
+Returns API status and version.
+
+**Authentication**: None
-**Request Body**:
```json
{
- "csrPem": "-----BEGIN CERTIFICATE REQUEST-----\nMIIC...\n-----END CERTIFICATE REQUEST-----",
- "status": "pending"
+ "status": "ok",
+ "version": "0.1.0"
}
```
-**Request Parameters**:
-- `csrPem` (string, required) - CSR in PEM format with proper line breaks
-- `status` (string, required) - Initial status (typically "pending")
+---
-**Validation Rules**:
-- CSR must be valid PEM format
-- CSR signature must be verifiable
-- Embedded public key must be RSA
-- RSA key must be at least 2048 bits
-- CSR cannot contain unauthorized domains (if authorization checks enabled)
+## Authentication & Profile
-**Response** (201 Created):
-```json
-{
- "id": 1,
- "status": "pending"
-}
-```
+### GET /auth/register
-**Response Parameters**:
-- `id` (number) - Database record ID for tracking certificate
-- `status` (string) - Initial status ("pending")
-
-**Error Responses**:
-
-| Code | Error | Cause |
-|------|-------|-------|
-| 400 | BadRequestException | Invalid CSR PEM format |
-| 400 | BadRequestException | CSR signature verification failed |
-| 400 | BadRequestException | Only RSA keys are supported currently |
-| 400 | BadRequestException | RSA key must be at least 2048 bits |
-| 400 | BadRequestException | CSR contains unauthorized domains |
-
-**Workflow**:
-1. CSR is parsed and validated
-2. Domains (SANs + CN) are extracted
-3. Record saved to database with status "pending"
-4. Job enqueued to `tlsCertIssuance` queue
-5. Async processing begins immediately
-
-**Job Queue Details**:
-- Queue: `tlsCertIssuance`
-- Max retries: 3
-- Backoff: exponential (5s initial delay)
-- Typical processing time: 2-5 minutes (dependent on DNS propagation)
+Redirects to Authentik enrollment flow for new user registration.
----
+**Authentication**: None
+
+### GET /auth/login
+
+Redirects to Authentik OIDC login page.
+
+**Authentication**: None
+
+### GET /auth/callback
+
+OIDC callback handler. Exchanges the authorization code for tokens and provisions the user on first login (JIT provisioning).
+
+**Query Parameters**: `code` (string)
+
+**Authentication**: None
-#### GET /certs/tls/{id}
-Retrieve certificate request details and status.
+### GET /auth/profile
-**Path Parameters**:
-- `id` (number, required) - Certificate record ID
+Returns the authenticated user's full profile including plan, resource counts, and organization info.
-**Response** (200 OK):
+**Response:**
```json
{
- "id": 1,
- "rawCsr": "-----BEGIN CERTIFICATE REQUEST-----\n...\n-----END CERTIFICATE REQUEST-----",
- "parsedCsr": {
- "subject": [...],
- "attributes": [...],
- "extensions": [...],
- "publicKeyLength": 2048
- },
- "crtPem": "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----",
- "status": "issued"
+ "userId": "authentik-sub-id",
+ "username": "alice",
+ "email": "alice@example.com",
+ "displayName": "Alice",
+ "groups": ["users"],
+ "plan": "team",
+ "domainCount": 5,
+ "certCount": 12,
+ "apiKeyCount": 2,
+ "organization": {
+ "id": "uuid",
+ "name": "My Team",
+ "role": "owner"
+ }
}
```
-**Response Parameters**:
-- `id` (number) - Record ID
-- `rawCsr` (string) - Original CSR in PEM format
-- `parsedCsr` (object) - Parsed CSR structure with certificate details
-- `crtPem` (string | null) - Issued certificate in PEM format (null until issued)
-- `status` (string) - Current status: "pending", "issuing", "issued", or "failed"
+### PATCH /auth/profile
-**Status Meanings**:
-- `pending` - CSR received, waiting to be processed
-- `issuing` - Certificate issuance in progress
-- `issued` - Certificate successfully issued and stored
-- `failed` - Issuance failed after max retries
+Update the current user's profile.
-**Error Responses**:
+**Request:**
+```json
+{
+ "displayName": "Alice Smith",
+ "notificationPreferences": {
+ "certExpiry": true,
+ "domainVerification": true
+ }
+}
+```
-| Code | Error | Cause |
-|------|-------|-------|
-| 404 | Not Found | Certificate record not found |
+### GET /auth/api-keys
----
+List all API keys for the current user. Returns metadata only (name, dates) — hashes are never exposed.
-#### PATCH /certs/tls/{id}
-Update certificate record (manual updates, typically for admin use).
+**Response:**
+```json
+[
+ {
+ "id": "uuid",
+ "name": "CI/CD Key",
+ "expiresAt": "2027-03-27T00:00:00.000Z",
+ "createdAt": "2026-03-27T10:00:00.000Z"
+ }
+]
+```
+
+### POST /auth/api-keys
-**Path Parameters**:
-- `id` (number, required) - Certificate record ID
+Generate a new API key.
-**Request Body**:
+**Request:**
```json
{
- "csrPem": "...",
- "status": "issued",
- "crtPem": "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----"
+ "name": "CI/CD Key",
+ "expiresAt": "2027-03-27T00:00:00.000Z"
}
```
-**Request Parameters** (all optional):
-- `csrPem` (string) - Updated CSR PEM
-- `status` (string) - Updated status
-- `crtPem` (string | null) - Updated certificate PEM
+Both fields are optional. `name` defaults to `"default"` (max 100 chars). `expiresAt` is an ISO 8601 date string.
-**Response** (200 OK):
+**Response:**
```json
{
- "id": 1,
- "rawCsr": "...",
- "parsedCsr": {...},
- "crtPem": "...",
- "status": "issued"
+ "id": "uuid",
+ "name": "CI/CD Key",
+ "apiKey": "kk_a1b2c3d4..."
}
```
-**Error Responses**:
+The `apiKey` value is shown **only once**. Store it securely.
-| Code | Error | Cause |
-|------|-------|-------|
-| 404 | Not Found | Certificate record not found |
+### DELETE /auth/api-keys/:id
+
+Delete an API key.
+
+### POST /auth/confirm-auto-renewal
+
+Confirm auto-renewal for free tier users. Required every 6 months to keep auto-renewal active.
---
-#### DELETE /certs/tls/{id}
-Revoke or remove a certificate.
+## Domains
+
+All endpoints require authentication. Write operations require `owner`, `admin`, or `member` role in an organization.
-**Path Parameters**:
-- `id` (number, required) - Certificate record ID
+### GET /domains
-**Response** (200 OK):
+List all domains for the current user (or organization).
+
+**Response:**
+```json
+[
+ {
+ "id": "uuid",
+ "hostname": "example.com",
+ "isVerified": true,
+ "verificationCode": "krakenkey-site-verification=abc123...",
+ "createdAt": "2026-03-27T10:00:00.000Z",
+ "updatedAt": "2026-03-27T10:00:00.000Z"
+ }
+]
+```
+
+### POST /domains
+
+Register a new domain.
+
+**Request:**
```json
{
- "message": "This action revokes a #1 tlsCrt"
+ "hostname": "example.com"
}
```
-**Note**: This endpoint is currently a stub. Actual revocation logic needs implementation.
+`hostname` must be a valid FQDN, max 253 characters. Subject to plan-based domain limits.
+
+**Response** (201): Domain object with `verificationCode` for DNS TXT setup.
+
+### GET /domains/:id
+
+Get a specific domain.
-**Error Responses**:
+### POST /domains/:id/verify
-| Code | Error | Cause |
-|------|-------|-------|
-| 404 | Not Found | Certificate record not found |
+Trigger DNS TXT verification for a domain. Checks for the verification code in DNS TXT records.
+
+**Response** (200): Updated domain object with `isVerified: true` on success.
+
+**Error** (400): Verification failed — TXT record not found or incorrect.
+
+### DELETE /domains/:id
+
+Remove a domain. Certificates already issued for this domain remain valid.
---
-## Data Types
+## Certificates
-### TlsCrt Object
-```typescript
-{
- id: number; // Primary key
- rawCsr: string; // Original CSR PEM
- parsedCsr: JSON; // Parsed CSR details
- crtPem: string | null; // Issued certificate
- status: string; // Status flag
-}
-```
+All endpoints require authentication. Write operations require `owner`, `admin`, or `member` role.
+
+### POST /certs/tls
-### CreateTlsCrtDto
-```typescript
+Submit a Certificate Signing Request for issuance.
+
+**Request:**
+```json
{
- csrPem: string; // CSR in PEM format
- status: string; // Initial status
+ "csrPem": "-----BEGIN CERTIFICATE REQUEST-----\nMIIC...\n-----END CERTIFICATE REQUEST-----"
}
```
-### UpdateTlsCrtDto
-```typescript
+**Validation:**
+- PEM format, max 10,000 characters
+- CSR signature verified against embedded public key
+- RSA (min 2048-bit) or ECDSA (P-256, P-384)
+- All domains (CN + SANs) must be verified in the user's account
+- Plan-based limits enforced (concurrent pending, total active, monthly quota)
+
+**Response** (201):
+```json
{
- csrPem?: string; // Optional CSR update
- status?: string; // Optional status update
- crtPem?: string | null; // Optional certificate update
+ "id": 42,
+ "status": "pending",
+ "parsedCsr": {
+ "subject": [{"shortName": "CN", "value": "example.com"}],
+ "extensions": [{"name": "subjectAltName", "altNames": [...]}],
+ "publicKeyLength": 4096
+ },
+ "createdAt": "2026-03-27T10:00:00.000Z"
}
```
----
+### GET /certs/tls
-## Common Request/Response Patterns
+List all certificates for the current user (or organization).
-### Successful CSR Submission
-```
-Request:
-POST /certs/tls
-Content-Type: application/json
+### GET /certs/tls/:id
-{
- "csrPem": "-----BEGIN CERTIFICATE REQUEST-----\n...",
- "status": "pending"
-}
+Get certificate details and status.
-Response:
-201 Created
+**Response:**
+```json
{
"id": 42,
- "status": "pending"
+ "status": "issued",
+ "parsedCsr": {...},
+ "crtPem": "-----BEGIN CERTIFICATE-----\n...\n-----END CERTIFICATE-----",
+ "expiresAt": "2026-06-25T10:00:00.000Z",
+ "autoRenew": true,
+ "renewalCount": 0,
+ "createdAt": "2026-03-27T10:00:00.000Z"
}
```
-### Polling for Certificate Status
-```
-Request:
-GET /certs/tls/42
+`rawCsr` and internal fields are excluded from API responses.
-Response:
-200 OK
+### GET /certs/tls/:id/details
+
+Get parsed certificate details (issuer, subject, key type/size, validity, fingerprint). Only available for `issued` certificates.
+
+### PATCH /certs/tls/:id
+
+Update certificate metadata.
+
+**Request:**
+```json
{
- "id": 42,
- "status": "issued",
- "crtPem": "-----BEGIN CERTIFICATE-----\n..."
+ "autoRenew": false
}
```
-### Typical Polling Intervals
-- Initial check: 10-30 seconds
-- Regular polling: 30-60 seconds
-- Max polling time: 10-15 minutes
+### POST /certs/tls/:id/renew
----
+Manually queue a renewal for an `issued` certificate. Creates a new ACME order using the original CSR.
-## Error Handling
+### POST /certs/tls/:id/retry
-### Error Response Format
+Retry issuance for a `failed` certificate. Re-queues the original CSR.
-All errors follow a consistent format:
+### POST /certs/tls/:id/revoke
+Revoke an issued certificate via ACME.
+
+**Request:**
```json
{
- "statusCode": 400,
- "message": "Invalid CSR PEM format",
- "error": "BadRequestException"
+ "reason": 1
}
```
-### Common HTTP Status Codes
+`reason` is an optional RFC 5280 revocation code (0–10). Default: 0 (unspecified).
-| Code | Meaning | When It Occurs |
-|------|---------|----------------|
-| 200 | OK | Successful GET/PATCH/DELETE |
-| 201 | Created | Successful POST |
-| 400 | Bad Request | Validation failure, invalid input |
-| 404 | Not Found | Resource doesn't exist |
-| 500 | Internal Server Error | Server-side exception |
+### DELETE /certs/tls/:id
----
+Delete a certificate record. Only `failed` or `revoked` certificates can be deleted.
-## Rate Limiting
+### Certificate Status Values
-Currently no rate limiting is implemented. For production use, consider:
-- Rate limiting per IP address
-- Rate limiting per user/API key
-- Burst allowance for legitimate clients
+| Status | Description |
+|--------|-------------|
+| `pending` | CSR received, job queued |
+| `issuing` | ACME workflow running |
+| `issued` | Certificate issued successfully |
+| `failed` | Failed after 3 retries |
+| `renewing` | Renewal in progress |
+| `revoking` | Revocation in progress |
+| `revoked` | Certificate revoked |
---
-## Authentication
+## Billing
-Authentication is handled via **Authentik (OIDC)** for users and **API Keys** for automation.
-All protected endpoints require a valid `Authorization` header.
+See [Billing](./BILLING.md) for full plan details and subscription lifecycle.
-**Header Format**:
-```
-Authorization: Bearer
-```
+### POST /billing/checkout
----
+Create a Stripe Checkout session. Returns a URL to redirect the user to.
-## CSR Format Requirements
+**Request:**
+```json
+{
+ "plan": "starter"
+}
+```
-### Valid CSR Format
-PEM format with proper line breaks (64 characters per line):
+Valid plans: `starter`, `team`, `business`, `enterprise`
-```
------BEGIN CERTIFICATE REQUEST-----
-MIICnjCCAYcCAQAwXTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUx
-ITAfBgNVBAoMGEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDEYMBYGA1UEAwwPZXhh
-...
------END CERTIFICATE REQUEST-----
-```
+### GET /billing/subscription
-### CSR Requirements
-- **Algorithm**: RSA (currently only supported)
-- **Key Size**: Minimum 2048 bits (4096 recommended)
-- **Signature**: Self-signed with private key
-- **Domains**: At least one domain (CN) with optional SANs
+Get the current user's (or organization's) subscription.
-### SANs Support
-The API automatically extracts both:
-- Common Name (CN) from subject
-- Subject Alternative Names (SANs) from extensions
+### POST /billing/portal
-Example domains:
-```
-CN: example.com
-SANs: www.example.com, api.example.com
-Result domains: [example.com, www.example.com, api.example.com]
-```
+Create a Stripe Customer Portal session for managing payment methods, invoices, and cancellation.
----
+### POST /billing/upgrade/preview
-## Certificate Status Lifecycle
+Preview the prorated cost of upgrading.
+**Request:**
+```json
+{
+ "plan": "business"
+}
```
- Submit CSR
- ↓
- +─────────────+
- │ pending │ (awaiting processing)
- +─────────────+
- ↓
- +─────────────+
- │ issuing │ (ACME workflow in progress)
- +─────────────+
- ↓
- ┌─────────┴──────────┐
- ↓ ↓
- +────────+ +─────────────+
- │ issued │ │ failed │
- +────────+ +─────────────+
- (success) (manual retry)
+
+**Response:**
+```json
+{
+ "currentPlan": "team",
+ "newPlan": "business",
+ "proratedAmount": 4500,
+ "currency": "usd"
+}
```
-**Possible Transitions**:
-- `pending` → `issuing` (automatic)
-- `issuing` → `issued` (automatic)
-- `issuing` → `failed` (after max retries)
-- Any status → manual update via PATCH
+### POST /billing/upgrade
----
+Execute a subscription upgrade. Charges the prorated difference immediately.
-## Webhook Support
+### POST /billing/webhook
-Not currently implemented. Future enhancement:
-- Webhook delivery on status changes
-- Event: cert.issued, cert.failed
-- Configurable callback URLs
+Stripe webhook endpoint. Verifies webhook signature and processes events. Not called by users directly.
---
-## Batch Operations
+## Organizations
-Not currently supported. Future enhancement:
-- Batch CSR submission
-- Bulk status check
-- Bulk operations endpoint
+See [Organizations](./ORGANIZATIONS.md) for full feature documentation.
----
+All endpoints require authentication.
-## Examples
-
-### JavaScript/Fetch
-
-```javascript
-// Submit CSR
-const response = await fetch('http://localhost:8080/certs/tls', {
- method: 'POST',
- headers: { 'Content-Type': 'application/json' },
- body: JSON.stringify({
- csrPem: csrPemString,
- status: 'pending'
- })
-});
-const { id, status } = await response.json();
-
-// Poll for status
-const pollInterval = setInterval(async () => {
- const statusResponse = await fetch(`http://localhost:8080/certs/tls/${id}`);
- const { status, crtPem } = await statusResponse.json();
-
- if (status === 'issued') {
- console.log('Certificate ready:', crtPem);
- clearInterval(pollInterval);
- }
-}, 5000);
-```
+### POST /organizations
-### cURL
+Create a new organization. Requires Team+ plan. The creating user becomes the owner.
-```bash
-# Submit CSR
-curl -X POST http://localhost:8080/certs/tls \
- -H "Content-Type: application/json" \
- -d '{
- "csrPem": "-----BEGIN CERTIFICATE REQUEST-----\n...",
- "status": "pending"
- }'
-
-# Get status
-curl http://localhost:8080/certs/tls/1
+**Request:**
+```json
+{
+ "name": "My Team"
+}
```
----
-
-## Debugging
+### GET /organizations/:id
-### Enable Request Logging
-Set environment variable: `DEBUG=nestjs:*`
+Get organization details with member list.
-### Database Query Logging
-Set in database config: `logging: ['query']`
+### POST /organizations/:id/members
-### ACME Debug Logging
-Check application logs for ACME client debug output
+Invite a member. Requires `owner` or `admin` role.
----
+**Request:**
+```json
+{
+ "email": "bob@example.com",
+ "role": "member"
+}
+```
-## Authentication & API Keys
+### DELETE /organizations/:id/members/:userId
-### Overview
+Remove a member. Admins can remove non-owners; members can remove themselves.
-KrakenKey uses **Authentik (OIDC)** for user authentication and **API Keys** for automation.
+### PATCH /organizations/:id
-#### Human Login Flow
-1. User visits application URL.
-2. Redirected to `https://auth.krakenkey.io` (Authentik).
-3. Authenticates and redirects back to `/auth/callback`.
-4. Backend swaps code for access token.
+Update organization name. Requires `owner` or `admin` role.
-### Endpoints
+### DELETE /organizations/:id
-#### GET /auth/login
-Initiates the OIDC login flow.
-**Response**: Redirects to Authentik. or returns redirect URL.
+Delete organization. Owner only. Queues async dissolution.
-#### GET /auth/register
-Initiates the OIDC registration flow.
-**Response**: Redirects to Authentik enrollment.
+### POST /organizations/:id/transfer-ownership
-#### GET /auth/callback
-Handles the OIDC callback.
-**Query Parameters**: `code` (string)
-**Response**: Returns auth tokens/session data.
+Transfer ownership to another member. Owner only.
-#### POST /auth/api-keys
-Generate a new API key.
-**Restricted**: Requires JWT authentication.
-
-**Headers:**
-```
-Authorization: Bearer
-```
-
-**Request Body:**
+**Request:**
```json
{
- "name": "default"
+ "email": "bob@example.com"
}
```
-**Response:**
+### PATCH /organizations/:id/members/:userId
+
+Update a member's role. Requires `owner` or `admin` role.
+
+**Request:**
```json
{
- "apiKey": "kk__abc123..."
+ "role": "admin"
}
```
-**Notes:**
-- The API key is shown **only once**.
-- Store it securely; it cannot be retrieved again.
-- The key is hashed before being stored in the database.
-
---
-#### `GET /auth/profile`
-Retrieve the authenticated user’s profile.
+## Endpoints (Monitoring)
-**Headers:**
-```
-Authorization: Bearer
-```
+See [Endpoints](./ENDPOINTS.md) for full feature documentation.
-**Response:**
+All endpoints require authentication.
+
+### POST /endpoints
+
+Create a monitored endpoint.
+
+**Request:**
```json
{
- "userId": "uuid",
- "username": "jdoe",
- "email": "jdoe@example.com",
- "groups": ["users"]
+ "host": "api.example.com",
+ "port": 443,
+ "label": "Production API"
}
```
----
+### GET /endpoints
-#### `Authorization: Bearer kk__...`
-Use API keys for automation.
+List all monitored endpoints.
-**Example:**
-```bash
-curl -H "Authorization: Bearer kk__abc123..." https://api.krakenkey.io/certs/tls
-```
+### GET /endpoints/:id
-**Validation:**
-- API keys are validated via SHA-256 hash lookup.
-- Invalid or revoked keys return `401 Unauthorized`.
+Get endpoint details.
----
+### PATCH /endpoints/:id
-### Security Notes
-- API keys are **long-lived** and should be rotated regularly.
-- Authentik access tokens are **short-lived** and used for user sessions.
-- Both authentication methods use the `Authorization: Bearer` header.
+Update endpoint settings.
----
+### DELETE /endpoints/:id
+
+Delete an endpoint.
-## User Management
+### POST /endpoints/:id/scan
-#### GET /users
-Retrieve all users.
+Request an immediate scan.
-**Response**: Array of User objects.
+### GET /endpoints/:id/results
-#### GET /users/{id}
-Retrieve a specific user.
+Get paginated scan results.
-#### PATCH /users/{id}
-Update a user.
+### GET /endpoints/:id/results/latest
-#### DELETE /users/{id}
-Delete a user.
+Get the latest result from each assigned probe.
+
+### GET /endpoints/:id/results/export
+
+Export results as CSV or JSON. Query parameter: `format=csv` or `format=json`.
+
+### GET /endpoints/probes/mine
+
+List available connected probes.
+
+### POST /endpoints/:id/probes
+
+Assign probes to an endpoint.
+
+### DELETE /endpoints/:id/probes/:probeId
+
+Unassign a probe.
+
+### POST /endpoints/:id/regions
+
+Add a hosted probe region.
+
+### DELETE /endpoints/:id/regions/:region
+
+Remove a hosted region.
---
-## Domain Management
+## Users
-#### GET /domains
-List all domains for the authenticated user.
+Admin-only endpoints except where noted.
-**Response**:
-```json
-[
- {
- "id": "uuid",
- "hostname": "example.com",
- "isVerified": false,
- "verificationCode": "krakenkey-site-verification=..."
- }
-]
-```
+### GET /users
-#### POST /domains
-Add a new domain.
+List all users. Admin only.
-**Request Body**:
-```json
-{
- "hostname": "example.com"
-}
-```
+### GET /users/:id
+
+Get a user. Accessible to the user themselves or admins.
-#### POST /domains/{id}/verify
-Trigger DNS verification for a domain.
+### PATCH /users/:id
+
+Update a user. Accessible to the user themselves or admins.
+
+### DELETE /users/:id
+
+Delete a user account. Cascades: revokes all certificates, deletes domains, anonymizes feedback, deletes API keys.
+
+Accessible to the user themselves or admins.
+
+---
+
+## Rate Limiting
+
+Rate limits are applied per IP address, per user, and per API key. Limits vary by endpoint category. When rate limited, the API returns `429 Too Many Requests` with a `Retry-After` header.
+
+---
+
+## CSR Format Requirements
-**Response**:
-Verified domain object or 400 Bad Request if verification fails.
+- **Format**: PEM with 64-character lines
+- **Key types**: RSA (min 2048-bit, recommended 4096) or ECDSA (P-256, P-384)
+- **Signature**: Must be self-signed with the corresponding private key
+- **Domains**: CN and/or SANs — all must be verified in your account
+- **Max size**: 10,000 characters
+- **Wildcards**: Supported (e.g. `*.example.com`) — requires base domain verification
-#### DELETE /domains/{id}
-Remove a domain.
+See [Certificate Flow](./CERTIFICATE_FLOW.md) for CSR generation examples.
diff --git a/backend/docs/ARCHITECTURE.md b/backend/docs/ARCHITECTURE.md
index 0b1f65e..3e6c9d7 100644
--- a/backend/docs/ARCHITECTURE.md
+++ b/backend/docs/ARCHITECTURE.md
@@ -2,507 +2,355 @@
## System Overview
-KrakenKey is a modular, service-oriented backend built with NestJS for managing TLS certificate lifecycle through ACME protocol automation.
+KrakenKey is a modular NestJS backend for TLS certificate lifecycle management, endpoint monitoring, and team collaboration.
```
-┌─────────────────────────────────────────────────────────────┐
-│ KrakenKey Backend │
-└─────────────────────────────────────────────────────────────┘
- │
- ┌──────────────────┼──────────────────┐
- │ │ │
- ┌───▼────┐ ┌───▼─────┐ ┌───▼──────┐
- │ App │ │ Certs │ │Auth/Users│
- │Module │ │ Module │ │ Modules │
- └────────┘ └───┬─────┘ └───┬──────┘
- │ │
- ┌───▼──────┐ ┌───▼─────────┐
- │ TLS │ │ Authentik │
- │ Module │ │ (OIDC) │
- └───┬──────┘ └─────────────┘
+┌──────────────────────────────────────────────────────────────────┐
+│ KrakenKey Backend │
+├──────────┬──────────┬──────────┬──────────┬──────────┬──────────┤
+│ Auth │ Certs │ Domains │ Billing │ Orgs │Endpoints │
+│ Module │ Module │ Module │ Module │ Module │ Module │
+└────┬─────┴────┬─────┴────┬─────┴────┬─────┴────┬─────┴────┬─────┘
+ │ │ │ │ │ │
+┌────▼───┐ ┌───▼────┐ ┌───▼──┐ ┌────▼───┐ ┌───▼──┐ ┌────▼────┐
+│Authentik│ │ ACME │ │ DNS │ │ Stripe │ │ DB │ │ Probes │
+│ (OIDC) │ │ (LE) │ │ (CF/ │ │ │ │ │ │ │
+│ │ │ │ │ R53) │ │ │ │ │ │ │
+└────────┘ └────────┘ └──────┘ └────────┘ └──────┘ └─────────┘
│
- ┌─────┴─────┐
- │ │
- ┌───────▼───┐ ┌────▼────────┐
- │ TLS │ │ Job Queue │
- │ Service │ │ (BullMQ) │
- └────┬──────┘ └─────┬──────┘
- │ │
- ┌────────┼───────────────┼────────┐
- │ │ │ │
- ┌───▼──┐ ┌──▼──┐ ┌─────────▼──┐ ┌─▼────┐
- │ACME │ │DNS │ │PostgreSQL │ │Redis │
- │Issuer│ │Prov.│ │ Database │ │Queue │
- └──────┘ └─────┘ └────────────┘ └──────┘
+ ┌─────┴──────┐
+ │ │
+ ┌─────▼──┐ ┌─────▼──┐
+ │ Redis │ │Postgres│
+ │(BullMQ)│ │(TypeORM│
+ └────────┘ └────────┘
```
## Module Hierarchy
### Root Module (AppModule)
-**File**: `src/app.module.ts`
-Configuration and initialization of the entire application.
+**File**: `src/app.module.ts`
**Imports**:
-- `AuthModule` - Authentication and OIDC integration
-- `UsersModule` - User management
-- `DomainsModule` - Domain verification
-- `CertsModule` - Certificate management functionality
-- `ConfigModule` - Environment variable loading (global)
-- `TypeOrmModule` - PostgreSQL database connection
-- `BullModule` - Redis-backed job queue initialization
-- `ScheduleModule` - NestJS cron scheduler (enables `@Cron` decorators globally)
-
-**Providers**:
-- `AppService` - Health check and version information
-- `AppController` - Root endpoint handler
+- `ConfigModule` — Global environment variable loading
+- `TypeOrmModule` — PostgreSQL connection with auto-running migrations
+- `BullModule` — Redis-backed job queue
+- `ScheduleModule` — Enables `@Cron` decorators globally
+- `AuthModule` — Authentication and OIDC
+- `UsersModule` — User management
+- `DomainsModule` — Domain verification
+- `CertsModule` — Certificate management
+- `BillingModule` — Stripe subscriptions and plan limits
+- `OrganizationsModule` — Team management
+- `EndpointsModule` — TLS endpoint monitoring
+- `HealthModule` — Health checks
+- `MetricsModule` — Prometheus metrics
+- `NotificationsModule` — Email notifications
+- `FeedbackModule` — User feedback
+- `ProbesModule` — Kubernetes probes
+- `ThrottlerModule` — Rate limiting
+
+**Global Guards**:
+- `RoleGuard` — Enforces role-based access control on all routes
-**Key Responsibilities**:
-- Initialize database connection with TypeORM
-- Load environment variables globally
-- Setup Redis connection for job queue
-- Bootstrap the application
+---
### Auth Module
-**File**: `src/auth/auth.module.ts`
-Handles authentication and authorization integration.
+**File**: `src/auth/auth.module.ts`
-**Imports**:
-- `UsersModule` - User persistence
-- `PassportModule` - Strategy execution
+Handles authentication via Authentik OIDC, JWT validation, and API key management.
**Providers**:
-- `AuthService` - OAuth/OIDC flow management
-- `JwtStrategy` - Validates Authentik JWTs
-- `ApiKeyStrategy` - Validates persistent API keys
+- `AuthService` — OIDC flows, API key CRUD, profile management, service key seeding
+- `JwtStrategy` — Validates Authentik JWTs via JWKS (RS256)
+- `ApiKeyStrategy` — Validates `kk_*` bearer tokens via scrypt hash lookup
+- `ServiceKeyStrategy` — Validates `kk_svc_*` service tokens
-**Key Responsibilities**:
-- OIDC Redirects & Callback handling
-- API Key generation and hashing
-- Request Guards (JwtOrApiKeyGuard)
+**Guards**:
+- `JwtOrApiKeyGuard` — Tries JWT first, falls back to API key
+- `AdminGuard` — Checks Authentik `groups` for admin membership
-### Users Module
-**File**: `src/users/users.module.ts`
+**Key behaviors**:
+- JIT user provisioning on first OIDC callback
+- API keys hashed with scrypt using `KK_HMAC_SECRET` as salt
+- Service key auto-seeded from `KK_PROBE_API_KEY` env var on startup
-Manages user identities.
+---
-**Imports**:
-- `TypeOrmModule` - User repository
+### Users Module
+
+**File**: `src/users/users.module.ts`
**Providers**:
-- `UsersService` - CRUD operations
+- `UsersService` — CRUD operations
+- `AccountDeletionService` — Cascading account deletion (revokes certs, deletes domains, anonymizes feedback)
-**Key Responsibilities**:
-- User persistence
-- Lookups by email/ID
+---
### Domains Module
+
**File**: `src/domains/domains.module.ts`
-Handles domain ownership verification.
+**Providers**:
+- `DomainsService` — Domain registration, DNS TXT verification, plan limit enforcement
+- `DomainsController` — REST endpoints
+- `DomainMonitorService` — Daily re-verification cron
+
+**Key behaviors**:
+- Generates unique verification codes on domain creation
+- Parent domain verification covers subdomains
+- Organization-scoped: org members share verified domains
+
+---
+
+### Certs Module → TLS Module
+
+**Files**: `src/certs/certs.module.ts`, `src/certs/tls/tls.module.ts`
**Providers**:
-- `DomainsService` - Verification logic (DNS TXT)
-- `DomainsController` - Endpoints
-- `DomainMonitorService` - Scheduled re-verification cron (`0 2 * * *`)
+- `TlsService` — CSR validation, certificate CRUD, plan limits, job queuing
+- `CsrUtilService` — CSR parsing, signature verification, domain extraction
+- `CertUtilService` — Certificate parsing (expiry, details, fingerprint)
+- `CertIssuerConsumer` — BullMQ job processor for issuance and renewal
+- `AcmeIssuerStrategy` — ACME protocol (orders, challenges, finalization)
+- `CloudflareDnsStrategy` — Cloudflare TXT record management
+- `Route53DnsStrategy` — AWS Route 53 TXT record management
+- `CertMonitorService` — Daily expiry monitoring cron
-**Key Responsibilities**:
-- Register domains for users
-- Verify ownership via DNS TXT records
-- Periodically re-check verified domains and revoke if TXT record is removed
+**DNS provider selection**: Factory pattern using `KK_DNS_PROVIDER` env var.
-### Certs Module
-**File**: `src/certs/certs.module.ts`
+---
-Container module for all certificate-related functionality.
+### Billing Module
-**Imports**:
-- `TlsModule` - TLS-specific operations
+**File**: `src/billing/billing.module.ts`
-**Exports**: None (used internally)
+**Providers**:
+- `BillingService` — Stripe checkout, portal, upgrades, webhook processing, tier resolution
+- `SubscriptionTierResolverService` — Resolves user's current plan (gracefully degrades to `free`)
+- `OrgDissolutionProcessor` — BullMQ processor for async organization dissolution
-**Key Responsibilities**:
-- Organize certificate-related features
-- Provide namespace for certificate endpoints (`/certs`)
+**Key behaviors**:
+- Plan limits enforced across all resource-creating modules
+- Flat-fee proration for upgrades (not day-based)
+- Organization subscriptions: personal ↔ org conversion on create/delete
+- Auto-dissolution of orgs when downgrading below Team tier
-### TLS Module
-**File**: `src/certs/tls/tls.module.ts`
+**Plan tiers**: `free` → `starter` → `team` → `business` → `enterprise`
-Core module for TLS certificate issuance and management.
+---
-**Imports**:
-- `TypeOrmModule` - TlsCrt entity repository
-- `BullModule` - Job queue for async processing
+### Organizations Module
+
+**File**: `src/organizations/organizations.module.ts`
**Providers**:
-- `TlsService` - Business logic
-- `CsrUtilService` - CSR parsing and validation
-- `CertUtilService` - Certificate parsing utilities
-- `CertIssuerConsumer` - Job processor (queue worker)
-- `AcmeIssuerStrategy` - ACME protocol implementation
-- `CloudflareDnsStrategy` - Cloudflare DNS integration
-- `Route53DnsStrategy` - AWS Route 53 DNS integration
-- `CertMonitorService` - Scheduled expiry monitoring cron (`0 6 * * *`)
-
-**Database Entities**:
-- `TlsCrt` - Certificate request record
-
-**Key Responsibilities**:
-- Handle CSR submission and validation
-- Manage certificate issuance workflow
-- Integrate with external ACME and DNS services
-- Process async jobs through BullMQ
-- Automatically queue renewal jobs for certificates expiring within 30 days
+- `OrganizationsService` — CRUD, member management, ownership transfer
+- `OrganizationsController` — REST endpoints with role-based access
+
+**Key behaviors**:
+- Role hierarchy: `owner` > `admin` > `member` > `viewer`
+- Requires Team+ plan to create
+- Users can only belong to one organization
+- Deletion queues async dissolution via BillingService
+
+---
+
+### Endpoints Module
+
+**File**: `src/endpoints/endpoints.module.ts`
+
+**Providers**:
+- `EndpointsService` — Endpoint CRUD, probe management, scan results, CSV/JSON export
+- `EndpointsController` — REST endpoints
+
+**Key behaviors**:
+- Dual scanning: managed (hosted) cloud probes and user-connected probes
+- Plan-based limits on endpoint count, hosted regions, hosted endpoints
+- Organization-scoped resource sharing
+
+---
## Data Flow Architecture
-### 1. Request Submission Flow
+### 1. Certificate Submission Flow
```
-Client CSR
- │
- ▼
+Client → POST /certs/tls
+ │
+ ▼
TlsController.create()
- │
- ▼
+ │
+ ▼
TlsService.create()
- ├─ CsrUtilService.validateAndParse()
- │ ├─ Verify CSR signature
- │ ├─ Extract domains (SANs + CN)
- │ ├─ Check key strength (min 2048 bits)
- │ └─ Parse to JSON
- ├─ Save to Database (TlsCrt entity)
- ├─ Enqueue Job to BullMQ
- │ ├─ Retry: 3 attempts
- │ └─ Backoff: exponential (5s initial)
- └─ Return { id, status: 'pending' }
+ ├─ CsrUtilService.validateAndParse()
+ │ ├─ Verify CSR signature
+ │ ├─ Extract domains (CN + SANs)
+ │ ├─ Validate key strength (RSA ≥2048, ECDSA P-256/P-384)
+ │ └─ Normalize PEM format
+ ├─ CsrUtilService.isAuthorized() — check domains against verified list
+ ├─ enforceCertLimits() — check plan quotas
+ ├─ Save TlsCrt to database (status: pending)
+ ├─ Enqueue tlsCertIssuance BullMQ job
+ └─ Return { id, status: 'pending' }
```
-### 2. Certificate Issuance Flow
+### 2. ACME Issuance Flow (Background)
```
-BullMQ Job Processing
- │
- ▼
+BullMQ picks up job
+ │
+ ▼
CertIssuerConsumer.process()
- ├─ Fetch CSR from Database
- ├─ Validate CSR Format
- ├─ Update Status: 'pending' → 'issuing'
- ├─ AcmeIssuerStrategy.issue()
- │ ├─ Initialize ACME Client
- │ ├─ Create Account (if needed)
- │ ├─ Extract Domains from CSR
- │ ├─ Create Order
- │ ├─ Get Authorizations & Challenges
- │ ├─ For each domain:
- │ │ ├─ CloudflareDnsStrategy.createRecord()
- │ │ ├─ Wait for DNS Propagation
- │ │ ├─ Notify ACME: Challenge Ready
- │ │ ├─ Poll Challenge Status
- │ │ └─ Clean up: removeRecord()
- │ ├─ Finalize Order with CSR
- │ ├─ Wait for CA Processing
- │ ├─ Retrieve Certificate PEM
- │ └─ Return certificate
- ├─ Update Status: 'issuing' → 'issued'
- ├─ Store Certificate in Database
- └─ Log Success
+ ├─ Fetch TlsCrt from DB
+ ├─ Update status → 'issuing'
+ ├─ AcmeIssuerStrategy.issue()
+ │ ├─ Initialize ACME client (account key from env)
+ │ ├─ Create ACME order for all domains
+ │ ├─ For each domain:
+ │ │ ├─ DnsStrategy.createRecord() — TXT at _acme-challenge.{domain}
+ │ │ ├─ waitForDns() — poll 15x at 10s intervals
+ │ │ └─ Complete ACME challenge
+ │ ├─ Finalize order with CSR
+ │ ├─ Retrieve certificate PEM
+ │ └─ DnsStrategy.removeRecord() — cleanup
+ ├─ Extract expiration date from certificate
+ ├─ Update TlsCrt: status → 'issued', store crtPem + expiresAt
+ ├─ Send success notification email
+ └─ Update metrics
```
-### 3. Status Query Flow
+### 3. Auto-Renewal Flow
```
-Client GET /certs/tls/:id
- │
- ▼
-TlsController.findOne()
- │
- ▼
-TlsService.findOne()
- │
- ▼
-Database Query (TlsCrt)
- │
- ▼
-Return Certificate Record with Status
+CertMonitorService (daily 06:00 UTC)
+ ├─ Query: status=issued, autoRenew=true, expiring within window
+ ├─ Filter by user tier (free: 5 days, paid: 30 days)
+ ├─ For each cert: TlsService.renewInternal()
+ │ ├─ Update status → 'renewing'
+ │ └─ Enqueue tlsCertRenewal BullMQ job
+ └─ Send expiry warning emails
```
-## Service Layer Architecture
-
-### TlsService
-**Responsibilities**:
-- CSR validation and persistence
-- Certificate record CRUD operations
-- Status management
-- Job enqueueing
-
-**Methods** (user-facing, ownership-checked):
-- `create(userId, createTlsCrtDto)` - Validate and queue CSR for issuance
-- `findAll(userId)` - List user's certificates
-- `findOne(id, userId)` - Retrieve one certificate
-- `update(id, userId, updateTlsCrtDto, status?)` - Update record
-- `renew(id, userId)` - Queue renewal for an `issued` certificate
-- `retry(id, userId)` - Re-queue issuance for a `failed` certificate
-- `remove(id, userId)` - Revoke certificate (stub)
-
-**Methods** (`@internal` — no ownership check, system use only):
-- `findOneInternal(id)` - Fetch cert without user scope (used by queue processors)
-- `updateInternal(id, dto, status?)` - Update cert without user scope (used by queue processors)
-- `renewInternal(id)` - Queue `tlsCertRenewal` job without user scope (used by `CertMonitorService`)
-
-### DomainsService
-**Responsibilities**:
-- Domain registration and ownership verification via DNS TXT lookup
-- Gating certificate issuance to verified domains only
-
-**Methods** (user-facing, ownership-checked):
-- `create(userId, createDomainDto)` - Register a domain and generate verification code
-- `findAll(userId)` - List user's domains
-- `findAllVerified(userId)` - List only verified domains (used by cert issuance auth check)
-- `findOne(id, userId)` - Retrieve one domain
-- `verify(userId, id)` - Perform DNS TXT lookup and mark as verified
-- `delete(userId, id)` - Remove a domain
-
-**Methods** (`@internal` — system use only):
-- `checkVerificationRecord(domain)` - DNS TXT lookup returning `boolean`; used by `DomainMonitorService` for periodic re-verification
-
-### CsrUtilService
-**Responsibilities**:
-- Parse and validate CSRs
-- Extract SANs and CN from CSR
-- Verify RSA key strength
-- Format PEM strings
-
-**Methods**:
-- `validateAndParse(pem)` - Complete CSR validation and parsing
-- `isAuthorized(dnsNames, allowedDomains)` - Domain authorization check
-- `formatPem(pem)` - Ensure correct PEM line wrapping
-
-### AcmeIssuerStrategy
-**Responsibilities**:
-- ACME protocol communication
-- Order creation and finalization
-- Challenge verification coordination
-- Certificate retrieval
-
-**Methods**:
-- `issue(csrPem, dnsProvider)` - Execute full issuance workflow
-- `waitForDns(recordName, expectedValue)` - Poll DNS propagation
-
-### CloudflareDnsStrategy
-**Responsibilities**:
-- Cloudflare API communication
-- DNS record creation and deletion
-- TXT record management
-
-**Methods**:
-- `createRecord(clientDomain, challengeToken)` - Create TXT record
-- `removeRecord(clientDomain)` - Delete TXT record
-
-## Database Schema
-
-### User Entity
-```typescript
-@Entity()
-export class User {
- @PrimaryGeneratedColumn('uuid')
- id: string;
-
- @Column({ unique: true })
- email: string;
-
- @Column({ nullable: true })
- username: string;
-
- @Column({ type: 'simple-array', default: [] })
- groups: string[];
-}
-```
-
-### Domain Entity
-```typescript
-@Entity()
-export class Domain {
- @PrimaryGeneratedColumn('uuid')
- id: string;
-
- @Column()
- hostname: string;
-
- @Column()
- verificationCode: string;
+### 4. Organization Dissolution Flow
- @Column({ default: false })
- isVerified: boolean;
-
- @ManyToOne(() => User)
- user: User;
-}
```
-
-### TlsCrt Entity
-```typescript
-@Entity()
-export class TlsCrt {
- @PrimaryGeneratedColumn()
- id: number;
-
- @Column()
- rawCsr: string; // Original PEM
-
- @Column('jsonb')
- parsedCsr: JSON; // Parsed structure
-
- @Column({ type: 'text', nullable: true })
- crtPem: string | null; // Issued certificate
-
- @Column({ default: 'pending', nullable: true })
- status: string; // pending|issuing|issued|failed
-}
+OrganizationsService.delete()
+ ├─ Set org status → 'dissolving'
+ └─ Enqueue org-dissolution BullMQ job
+ │
+ ▼
+OrgDissolutionProcessor.process()
+ ├─ Transfer non-owner member resources to owner
+ ├─ Clear member org associations
+ ├─ Convert org subscription → personal subscription
+ └─ Delete organization record
```
-**Statuses**:
-- `pending` - CSR received, awaiting processing
-- `issuing` - Certificate issuance in progress
-- `issued` - Certificate successfully issued
-- `failed` - Issuance failed (manual intervention may be needed)
+---
## Scheduled Jobs
-All cron jobs are managed by `@nestjs/schedule` (`ScheduleModule.forRoot()` registered in `AppModule`). Jobs run in-process — no separate worker process is required.
-
| Service | Cron | Time | Action |
|---------|------|------|--------|
-| `DomainMonitorService` | `0 2 * * *` | Daily 02:00 UTC | Re-checks TXT record for all verified domains; marks unverified if record is absent |
-| `CertMonitorService` | `0 6 * * *` | Daily 06:00 UTC | Finds `issued` certs expiring within 30 days; queues `tlsCertRenewal` BullMQ jobs |
+| `DomainMonitorService` | `0 2 * * *` | 02:00 UTC | Re-verify DNS TXT for all verified domains. Marks unverified if missing. Sends email notification |
+| `CertMonitorService` | `0 6 * * *` | 06:00 UTC | Find expiring certs, queue renewal jobs, send warning emails, update metrics |
+
+**Ordering**: Domain re-verification runs at 2AM so revoked domains block cert operations before the 6AM cert monitor runs.
-**Ordering rationale**: Domain re-verification runs at 2AM so any domains that have lost their TXT record are marked unverified before the cert monitor runs at 6AM. New certificate submissions for those domains will be blocked immediately after 2AM.
+Both services use per-item try/catch — one failure does not abort the batch.
-### DomainMonitorService
+---
-- **File**: `src/domains/services/domain-monitor.service.ts`
-- **Query**: All domains where `isVerified = true`
-- **Check**: `resolveTxt(hostname)` — looks for `verificationCode` in flattened TXT records
-- **On failure**: Updates `isVerified = false`; logs a warning with domain hostname and ID
-- **On DNS error**: Returns `false` (cautious — transient DNS failures will revoke verification)
-- **Error isolation**: Per-domain try/catch so one failure does not abort the rest of the batch
+## Queue Architecture (BullMQ)
-### CertMonitorService
+| Queue | Purpose | Retry | Backoff |
+|-------|---------|-------|---------|
+| `tlsCertIssuance` | Initial certificate issuance | 3 attempts | Exponential (5s base) |
+| `tlsCertRenewal` | Certificate renewal | 3 attempts | Exponential (5s base) |
+| `org-dissolution` | Async organization deletion | 3 attempts | Exponential |
-- **File**: `src/certs/tls/services/cert-monitor.service.ts`
-- **Query**: `status = 'issued' AND expiresAt < now() + 30 days` (DB-level filter via TypeORM `LessThan`)
-- **Action**: Calls `TlsService.renewInternal(certId)` for each result
-- **renewInternal**: Updates status to `renewing`, enqueues `tlsCertRenewal` BullMQ job
-- **Error isolation**: Per-cert try/catch so one failure does not abort the rest of the batch
+All queues backed by Redis. Job payload is `{ certId }` for cert queues and `{ orgId }` for dissolution.
---
-## Queue Architecture
-
-### Job Queues (BullMQ)
+## Authentication Architecture
-**Queue**: `tlsCertIssuance` — initial certificate issuance
+```
+┌──────────────────────────────────────────────┐
+│ Request │
+│ Authorization: Bearer │
+└──────────────────┬───────────────────────────┘
+ │
+ ┌──────▼──────┐
+ │JwtOrApiKey │
+ │ Guard │
+ └──────┬──────┘
+ │
+ ┌─────────┴─────────┐
+ ▼ ▼
+ ┌───────────┐ ┌───────────┐
+ │JWT Strategy│ │API Key │
+ │(Authentik) │ │Strategy │
+ │RS256 + JWKS│ │kk_* hash │
+ └─────┬─────┘ └─────┬─────┘
+ │ │
+ └─────────┬─────────┘
+ ▼
+ ┌─────────────┐
+ │ RoleGuard │
+ │(org roles) │
+ └──────┬──────┘
+ ▼
+ ┌─────────────┐
+ │ Controller │
+ └─────────────┘
+```
-**Queue**: `tlsCertRenewal` — certificate renewal (triggered by user or `CertMonitorService`)
+**API key types**:
-**Job Structure** (both queues):
-```typescript
-{
- certId: number // ID of the TlsCrt database record
-}
-```
+| Prefix | Type | Purpose |
+|--------|------|---------|
+| `kk_` | User API key | Individual user access |
+| `kk_svc_` | Service key | System-level (probes, internal services) |
-**Retry Policy** (both queues):
-- Max attempts: 3
-- Backoff strategy: exponential
-- Initial delay: 5000ms
-- Schedule: attempt 1 → immediate, attempt 2 → ~5s, attempt 3 → ~25s
-
-**Processor**: `CertIssuerConsumer`
-
-## External Service Integration
-
-### Let's Encrypt ACME
-- **Purpose**: Certificate Authority
-- **Environment**: Staging (default) or Production
-- **Key Workflow**:
- 1. Account creation/reuse
- 2. Order creation for domains
- 3. Authorization retrieval
- 4. Challenge response
- 5. Order finalization
- 6. Certificate download
-
-### Cloudflare DNS
-- **Purpose**: DNS provider for DNS-01 challenges
-- **Key Operations**:
- 1. Create TXT record with challenge token
- 2. Verify propagation
- 3. Delete record after verification
-- **Configuration**: Zone ID and API token via environment
-
-## Error Handling
-
-### CSR Validation Errors
-- Invalid PEM format → `BadRequestException`
-- Signature verification failure → `BadRequestException`
-- Key too small → `BadRequestException`
-- Unauthorized domains → `BadRequestException`
-
-### ACME Errors
-- Challenge setup failure → Job retry with backoff
-- DNS propagation timeout → Job failure
-- Certificate retrieval failure → Job retry
-
-### Job Processing
-- Max retries: 3 with exponential backoff
-- Failed jobs logged for manual review
-- Status updated to `failed` after all retries exhausted
-
-## Scalability Considerations
-
-1. **Horizontal Scaling**:
- - Stateless service design
- - Redis-backed queue for distributed processing
- - PostgreSQL for shared state
-
-2. **Performance Optimization**:
- - Async/await for non-blocking I/O
- - BullMQ for parallel job processing
- - Connection pooling via TypeORM
-
-3. **Resource Management**:
- - Graceful shutdown handling
- - Configurable timeouts
- - Exponential backoff to prevent thundering herd
+---
## Security Architecture
-1. **CSR Validation**:
- - Signature verification using embedded public key
- - Key strength validation (min 2048 bits RSA)
- - Domain authorization through DNS challenges
+1. **CSR validation**: Signature verification, key strength checks, domain authorization
+2. **API key hashing**: scrypt with `KK_HMAC_SECRET` salt — keys cannot be verified without the secret
+3. **OIDC**: JWT validated via JWKS endpoint, RS256 algorithm, issuer verification
+4. **Role-based access**: Global `RoleGuard` enforces org roles on all routes
+5. **Rate limiting**: Per IP, per user, per API key via `ThrottlerModule`
+6. **Webhook verification**: Stripe webhook signatures verified before processing
+7. **Helmet**: Security headers enabled globally
+8. **CORS**: Domain-based whitelist
+9. **Validation**: Global `ValidationPipe` with whitelist and transform
-2. **Credentials Management**:
- - ACME account key via environment variable
- - Cloudflare API token via environment variable
- - No hardcoded secrets
+---
+
+## Configuration
-3. **DNS Challenge**:
- - DNS-01 challenge for domain validation
- - TXT record creation/deletion
- - DNS propagation verification before ACME notification
+All configuration via environment variables, loaded globally by `ConfigModule`. See [Configuration](./CONFIGURATION.md) for the full reference.
-## Configuration Management
+**Variable prefixes**:
+- `KK_DB_*` — PostgreSQL
+- `KK_BULLMQ_*` — Redis
+- `KK_AUTHENTIK_*` — OIDC provider
+- `KK_STRIPE_*` — Billing
+- `CLOUDFLARE_*` — DNS provider
+- `AWS_*` / `KK_AWS_*` — Route 53
+- `ACME_*` — Let's Encrypt
-**Global Configuration Module**: `ConfigModule.forRoot({ isGlobal: true })`
+---
-**Environment Variables**:
-- Database: `TYPEORM_*`
-- Redis: `BULLMQ_*`
-- ACME: `ACME_*`
-- Cloudflare: `CLOUDFLARE_*`
-- API: `PORT`, `API_VERSION`
+## Scalability
-All configuration loaded at startup and injected via `ConfigService`.
+- **Stateless**: No in-process state — horizontally scalable behind a load balancer
+- **Async processing**: BullMQ distributes cert issuance across workers
+- **Connection pooling**: TypeORM manages database connections
+- **Redis-backed queues**: Shared job state across instances
+- **Graceful shutdown**: Properly drains connections and jobs
diff --git a/backend/docs/BILLING.md b/backend/docs/BILLING.md
new file mode 100644
index 0000000..4143c1c
--- /dev/null
+++ b/backend/docs/BILLING.md
@@ -0,0 +1,198 @@
+# Billing & Subscriptions
+
+KrakenKey uses Stripe for subscription management with five plan tiers that control feature access and resource limits.
+
+## Plan Tiers
+
+| Feature | Free | Starter | Team | Business | Enterprise |
+|---------|------|---------|------|----------|------------|
+| **Price** | $0 | — | — | — | Contact us |
+| **Domains** | 3 | 10 | 25 | 75 | Unlimited |
+| **API Keys** | 2 | 5 | 10 | 25 | Unlimited |
+| **Certificates / month** | 5 | 50 | 250 | 1,000 | Unlimited |
+| **Active Certificates** | 10 | 75 | 375 | 1,500 | Unlimited |
+| **Scan Interval** | 60 min | 30 min | 5 min | 1 min | Custom |
+| **Hosted Probe Regions** | — | — | 5 | 15 | Unlimited |
+| **Hosted Endpoints** | — | — | — | 100 | Unlimited |
+| **Data Retention** | 5 days | 30 days | 90 days | 90 days | Custom |
+| **Auto-Renewal Window** | 5 days | 30 days | 30 days | 30 days | 30 days |
+| **Organizations** | — | — | Yes | Yes | Yes |
+
+## API Endpoints
+
+### Create Checkout Session
+
+Start a new subscription by redirecting the user to Stripe Checkout.
+
+```
+POST /billing/checkout
+```
+
+**Request:**
+```json
+{
+ "plan": "starter"
+}
+```
+
+**Response:**
+```json
+{
+ "url": "https://checkout.stripe.com/c/pay_..."
+}
+```
+
+Redirect the user to the returned URL. After payment, Stripe redirects back to KrakenKey and fires a `checkout.session.completed` webhook.
+
+Valid plan values: `starter`, `team`, `business`, `enterprise`
+
+### Get Current Subscription
+
+```
+GET /billing/subscription
+```
+
+**Response:**
+```json
+{
+ "plan": "team",
+ "status": "active",
+ "stripeCustomerId": "cus_...",
+ "stripeSubscriptionId": "sub_...",
+ "currentPeriodStart": "2026-03-01T00:00:00.000Z",
+ "currentPeriodEnd": "2026-04-01T00:00:00.000Z"
+}
+```
+
+Returns `null` for users on the free tier (no active subscription).
+
+### Manage Subscription (Stripe Portal)
+
+Opens the Stripe Customer Portal where users can update payment methods, view invoices, and cancel.
+
+```
+POST /billing/portal
+```
+
+**Response:**
+```json
+{
+ "url": "https://billing.stripe.com/p/session/..."
+}
+```
+
+### Preview Upgrade
+
+See the prorated cost before upgrading.
+
+```
+POST /billing/upgrade/preview
+```
+
+**Request:**
+```json
+{
+ "plan": "business"
+}
+```
+
+**Response:**
+```json
+{
+ "currentPlan": "team",
+ "newPlan": "business",
+ "proratedAmount": 4500,
+ "currency": "usd"
+}
+```
+
+The `proratedAmount` is a flat fee difference (not day-based proration) in the smallest currency unit (cents for USD).
+
+### Execute Upgrade
+
+```
+POST /billing/upgrade
+```
+
+**Request:**
+```json
+{
+ "plan": "business"
+}
+```
+
+Immediately charges the prorated difference and switches the subscription to the new plan.
+
+### Stripe Webhook
+
+```
+POST /billing/webhook
+```
+
+Internal endpoint — receives signed webhook events from Stripe. Not called by users directly. See [Integrations](./INTEGRATIONS.md#stripe-billing) for webhook setup.
+
+## Subscription Lifecycle
+
+### New Subscription
+1. User selects a plan and clicks upgrade
+2. Backend creates a Stripe Checkout Session
+3. User completes payment on Stripe
+4. `checkout.session.completed` webhook fires
+5. Backend creates a `Subscription` record linked to the user (or organization)
+
+### Plan Upgrade
+1. User requests upgrade preview to see prorated cost
+2. User confirms upgrade
+3. Backend charges flat-fee proration via Stripe
+4. `customer.subscription.updated` webhook fires
+5. Backend updates the local subscription record
+
+### Cancellation
+1. User cancels via Stripe Customer Portal
+2. `customer.subscription.deleted` webhook fires
+3. Backend marks subscription as canceled
+4. User reverts to free tier limits
+
+### Failed Payment
+1. `invoice.payment_failed` webhook fires
+2. Backend marks subscription as `past_due`
+3. Stripe retries payment per its dunning settings
+4. If retries succeed, `customer.subscription.updated` restores `active` status
+
+## Organization Billing
+
+Organizations have their own billing relationship:
+
+- When a user creates an organization (requires Team+ plan), their personal subscription is converted to an organization subscription
+- The organization owner controls billing — non-owners cannot access billing endpoints
+- If an organization is downgraded below the Team tier, the organization is automatically dissolved:
+ 1. Non-owner member resources (domains, certificates) are transferred to the organization owner
+ 2. Member associations are cleared
+ 3. The subscription reverts to a personal subscription
+ 4. The organization is deleted
+
+## Subscription Statuses
+
+| Status | Description |
+|--------|-------------|
+| `active` | Subscription is current and payment is up to date |
+| `past_due` | Payment failed, Stripe is retrying |
+| `canceled` | Subscription has been canceled |
+| `incomplete` | Initial payment has not completed |
+| `trialing` | Subscription is in a trial period |
+
+## Subscription Entity Schema
+
+| Column | Type | Description |
+|--------|------|-------------|
+| `id` | UUID | Primary key |
+| `plan` | varchar | Plan name (starter, team, business, enterprise) |
+| `status` | varchar | Subscription status |
+| `stripeCustomerId` | varchar | Stripe customer ID |
+| `stripeSubscriptionId` | varchar | Stripe subscription ID (unique, indexed) |
+| `currentPeriodStart` | timestamp | Current billing period start |
+| `currentPeriodEnd` | timestamp | Current billing period end |
+| `userId` | varchar | Owner user ID (nullable, indexed) |
+| `organizationId` | UUID | Owner organization ID (nullable, indexed) |
+| `createdAt` | timestamp | Record creation time |
+| `updatedAt` | timestamp | Last update time |
diff --git a/backend/docs/CERTIFICATE_FLOW.md b/backend/docs/CERTIFICATE_FLOW.md
new file mode 100644
index 0000000..d4f0688
--- /dev/null
+++ b/backend/docs/CERTIFICATE_FLOW.md
@@ -0,0 +1,363 @@
+# Certificate Issuance Flow
+
+End-to-end walkthrough of how KrakenKey processes certificate requests, from CSR submission through ACME issuance to automated renewal.
+
+## Certificate Lifecycle
+
+```
+ submit CSR
+ │
+ ▼
+ ┌─────────┐
+ │ pending │
+ └────┬─────┘
+ │ BullMQ job picked up
+ ▼
+ ┌─────────┐
+ │ issuing │──── ACME DNS-01 challenge ────┐
+ └────┬─────┘ │
+ │ │
+ success │ failure (after 3 retries)
+ │ │
+ ▼ ▼
+ ┌─────────┐ ┌──────────┐
+ │ issued │ │ failed │
+ └────┬─────┘ └──────────┘
+ │ │
+ expiring │ (auto-renew) retry │ (manual)
+ ▼ │
+ ┌──────────┐ │
+ │ renewing │◄─────────────────────────────┘
+ └────┬──────┘
+ │
+ ▼
+ ┌─────────┐
+ │ issued │ (renewed)
+ └────┬─────┘
+ │
+ revoke │ (manual)
+ ▼
+ ┌──────────┐
+ │ revoking │
+ └────┬──────┘
+ │
+ ▼
+ ┌─────────┐
+ │ revoked │
+ └──────────┘
+```
+
+### Status Values
+
+| Status | Description |
+|--------|-------------|
+| `pending` | CSR received, validated, and queued for processing |
+| `issuing` | ACME workflow actively running (order created, challenges in progress) |
+| `issued` | Certificate successfully issued and stored |
+| `failed` | Issuance failed after 3 retry attempts |
+| `renewing` | Renewal job in progress for an expiring certificate |
+| `revoking` | Revocation request sent to ACME CA |
+| `revoked` | Certificate successfully revoked |
+
+---
+
+## Step 1: Generate a CSR
+
+Before submitting to KrakenKey, generate a Certificate Signing Request using OpenSSL.
+
+### Single Domain
+
+```bash
+# Generate private key
+openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -out domain.key
+
+# Generate CSR
+openssl req -new -key domain.key -out domain.csr \
+ -subj "/CN=example.com"
+```
+
+### Multiple Domains (SANs)
+
+```bash
+# Generate private key
+openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -out domain.key
+
+# Generate CSR with Subject Alternative Names
+openssl req -new -key domain.key -out domain.csr \
+ -subj "/CN=example.com" \
+ -addext "subjectAltName=DNS:example.com,DNS:www.example.com,DNS:api.example.com"
+```
+
+### Wildcard Certificate
+
+```bash
+openssl req -new -key domain.key -out domain.csr \
+ -subj "/CN=*.example.com" \
+ -addext "subjectAltName=DNS:*.example.com,DNS:example.com"
+```
+
+### ECDSA Key (Alternative to RSA)
+
+```bash
+# Generate ECDSA key (P-256 or P-384)
+openssl ecparam -genkey -name prime256v1 -out domain.key
+
+# Generate CSR
+openssl req -new -key domain.key -out domain.csr \
+ -subj "/CN=example.com"
+```
+
+### CSR Requirements
+
+| Requirement | Details |
+|-------------|---------|
+| Format | PEM-encoded, 64-character line width |
+| Key types | RSA (min 2048-bit, recommended 4096) or ECDSA (P-256, P-384) |
+| Signature | CSR must be self-signed with the corresponding private key |
+| Max size | 10,000 characters |
+| Domains | All domains in the CSR (CN + SANs) must be verified in your account |
+
+---
+
+## Step 2: Verify Domain Ownership
+
+Every domain in the CSR must be verified before a certificate can be issued. See the [Domain Verification Guide](../../docs/DOMAIN_VERIFICATION_GUIDE.md) for full instructions.
+
+**Key points:**
+- Add a DNS TXT record with the verification code provided by KrakenKey
+- Parent domain verification covers subdomains (verifying `example.com` authorizes `sub.example.com`)
+- Wildcard certificates require the base domain to be verified
+- TXT records must remain in DNS — a daily cron job at 02:00 UTC re-verifies all domains
+
+---
+
+## Step 3: Submit the CSR
+
+### Via API
+
+```bash
+# Read CSR file content
+CSR_PEM=$(cat domain.csr)
+
+# Submit CSR
+curl -X POST https://api.example.com/certs/tls \
+ -H "Authorization: Bearer $API_KEY" \
+ -H "Content-Type: application/json" \
+ -d "{\"csrPem\": \"$CSR_PEM\"}"
+```
+
+**Response:**
+
+```json
+{
+ "id": 42,
+ "status": "pending",
+ "parsedCsr": {
+ "subject": [{"shortName": "CN", "value": "example.com"}],
+ "extensions": [{"name": "subjectAltName", "altNames": [{"type": 2, "value": "example.com"}]}],
+ "publicKeyLength": 4096
+ },
+ "createdAt": "2026-03-27T10:00:00.000Z"
+}
+```
+
+### What Happens on Submission
+
+1. **CSR Validation** — Signature verified, key strength checked, PEM format normalized
+2. **Domain Authorization** — All domains in the CSR checked against user's verified domains
+3. **Plan Limits** — Concurrent pending, total active, and monthly certificate quotas enforced
+4. **Job Queued** — A `tlsCertIssuance` job is added to the BullMQ queue
+5. **Status: `pending`** — Certificate record created in database
+
+---
+
+## Step 4: ACME Issuance (Background)
+
+The BullMQ job processor handles issuance asynchronously:
+
+```
+┌──────────────┐ ┌──────────────┐ ┌──────────────────┐
+│ Create ACME │────▶│ Create DNS │────▶│ Wait for DNS │
+│ Order │ │ TXT Record │ │ Propagation │
+└──────────────┘ └──────────────┘ └────────┬─────────┘
+ │
+ 15 attempts × 10s
+ │
+ ▼
+┌──────────────┐ ┌──────────────┐ ┌──────────────────┐
+│ Store Cert │◄────│ Finalize │◄────│ Complete │
+│ in DB │ │ Order │ │ Challenge │
+└──────────────┘ └──────────────┘ └──────────────────┘
+ │
+ ▼
+ Clean up DNS records
+ Send success email
+ Update metrics
+```
+
+### DNS-01 Challenge Process
+
+1. **ACME order created** with Let's Encrypt for all domains in the CSR
+2. **For each domain**, a TXT record is created:
+ - Record name: `_acme-challenge.{domain}` (dots flattened to dashes in the hostname)
+ - Record value: ACME challenge token
+ - TTL: 60 seconds
+ - Zone: The configured `ACME_AUTH_ZONE_DOMAIN`
+3. **DNS propagation polling**: Up to 15 attempts at 10-second intervals
+4. **Challenge completed** with the ACME server
+5. **Order finalized** with the original CSR
+6. **Certificate PEM** retrieved and stored
+7. **DNS TXT records cleaned up**
+
+### Retry Policy
+
+| Setting | Value |
+|---------|-------|
+| Max retries | 3 |
+| Backoff | Exponential (5-second base delay) |
+| Retry delays | ~5s, ~25s, ~125s |
+
+If all retries fail, the certificate status is set to `failed` and a failure notification email is sent.
+
+---
+
+## Step 5: Retrieve the Certificate
+
+### Poll for Status
+
+```bash
+curl https://api.example.com/certs/tls/42 \
+ -H "Authorization: Bearer $API_KEY"
+```
+
+**Response (issued):**
+
+```json
+{
+ "id": 42,
+ "status": "issued",
+ "crtPem": "-----BEGIN CERTIFICATE-----\nMIIE...\n-----END CERTIFICATE-----\n",
+ "parsedCsr": { ... },
+ "expiresAt": "2026-06-25T10:00:00.000Z",
+ "autoRenew": true,
+ "renewalCount": 0,
+ "createdAt": "2026-03-27T10:00:00.000Z"
+}
+```
+
+### Get Certificate Details
+
+```bash
+curl https://api.example.com/certs/tls/42/details \
+ -H "Authorization: Bearer $API_KEY"
+```
+
+Returns parsed certificate information including issuer, subject, key type/size, validity period, and fingerprint.
+
+---
+
+## Auto-Renewal
+
+KrakenKey automatically monitors and renews certificates.
+
+### How It Works
+
+- **CertMonitorService** runs daily at **06:00 UTC**
+- Finds all `issued` certificates with `autoRenew: true` that are expiring within the renewal window
+- Queues renewal jobs to the `tlsCertRenewal` BullMQ queue
+- Sends expiry warning emails for certificates approaching expiration
+
+### Renewal Windows
+
+| Plan | Renewal Window |
+|------|---------------|
+| Free | 5 days before expiry |
+| Starter, Team, Business, Enterprise | 30 days before expiry |
+
+### Free Tier Confirmation
+
+Free tier users must confirm auto-renewal every 6 months by calling:
+
+```bash
+curl -X POST https://api.example.com/auth/confirm-auto-renewal \
+ -H "Authorization: Bearer $API_KEY"
+```
+
+If not confirmed within 6 months, auto-renewal is paused until re-confirmed.
+
+### Manual Renewal
+
+```bash
+curl -X POST https://api.example.com/certs/tls/42/renew \
+ -H "Authorization: Bearer $API_KEY"
+```
+
+### Disabling Auto-Renewal
+
+```bash
+curl -X PATCH https://api.example.com/certs/tls/42 \
+ -H "Authorization: Bearer $API_KEY" \
+ -H "Content-Type: application/json" \
+ -d '{"autoRenew": false}'
+```
+
+---
+
+## Revocation
+
+Revoke a certificate when the private key is compromised or the certificate is no longer needed.
+
+```bash
+curl -X POST https://api.example.com/certs/tls/42/revoke \
+ -H "Authorization: Bearer $API_KEY" \
+ -H "Content-Type: application/json" \
+ -d '{"reason": 1}'
+```
+
+### RFC 5280 Revocation Reason Codes
+
+| Code | Reason |
+|------|--------|
+| 0 | Unspecified |
+| 1 | Key compromise |
+| 2 | CA compromise |
+| 3 | Affiliation changed |
+| 4 | Superseded |
+| 5 | Cessation of operation |
+| 9 | Privilege withdrawn |
+| 10 | AA compromise |
+
+Revoked and failed certificates can be deleted from your account:
+
+```bash
+curl -X DELETE https://api.example.com/certs/tls/42 \
+ -H "Authorization: Bearer $API_KEY"
+```
+
+Only certificates in `failed` or `revoked` status can be deleted.
+
+---
+
+## Retrying Failed Certificates
+
+If issuance failed (e.g. DNS propagation timeout), you can retry:
+
+```bash
+curl -X POST https://api.example.com/certs/tls/42/retry \
+ -H "Authorization: Bearer $API_KEY"
+```
+
+This re-queues the original CSR for another issuance attempt.
+
+---
+
+## Plan Limits
+
+Certificate operations are subject to plan-based quotas:
+
+| Limit | Free | Starter | Team | Business | Enterprise |
+|-------|------|---------|------|----------|------------|
+| Certificates per month | 5 | 50 | 250 | 1,000 | Unlimited |
+| Active certificates | 10 | 75 | 375 | 1,500 | Unlimited |
+
+See [Billing](./BILLING.md) for full plan details.
diff --git a/backend/docs/CONFIGURATION.md b/backend/docs/CONFIGURATION.md
new file mode 100644
index 0000000..8b05bf1
--- /dev/null
+++ b/backend/docs/CONFIGURATION.md
@@ -0,0 +1,181 @@
+# Configuration Guide
+
+Complete reference for all environment variables used by the KrakenKey backend and frontend.
+
+## Backend Environment Variables
+
+### General
+
+| Variable | Required | Default | Description |
+|----------|----------|---------|-------------|
+| `API_VERSION` | No | `0.0.1` | API version string returned in health check |
+| `NODE_ENV` | No | `dev` | Environment mode (`dev`, `production`, `test`) |
+| `PORT` | No | `8888` | Internal port (unused by NestJS listener) |
+| `KK_API_PORT` | No | `8080` | Port the NestJS API listens on |
+
+### Domain Configuration
+
+| Variable | Required | Default | Description |
+|----------|----------|---------|-------------|
+| `KK_APP_DOMAIN` | Yes | — | Web app domain. Must be a single-level subdomain for Cloudflare Free tier proxy support (e.g. `dev-web.krakenkey.io`) |
+| `ACME_AUTH_ZONE_DOMAIN` | Yes | — | DNS zone used for ACME DNS-01 challenge delegation (e.g. `acme.krakenkey.io`). Can be multi-level since it's DNS-only (no SSL proxy needed) |
+| `ACME_CONTACT_EMAIL` | Yes | — | Email registered with Let's Encrypt for certificate expiry notifications |
+
+### PostgreSQL Database
+
+| Variable | Required | Default | Description |
+|----------|----------|---------|-------------|
+| `KK_DB_HOST` | Yes | — | Database hostname (e.g. `krakenkey-db-dev` in Docker, `localhost` otherwise) |
+| `KK_DB_PORT` | No | `5432` | Database port |
+| `KK_DB_USERNAME` | Yes | — | Database user |
+| `KK_DB_PASSWORD` | Yes | — | Database password |
+| `KK_DB_DATABASE` | No | `krakenkey` | Database name |
+| `KK_DB_LOGGING` | No | `false` | Enable TypeORM SQL query logging (`true`/`false`) |
+| `KK_DB_SYNCHRONIZE` | No | `false` | Auto-sync schema from entities. **Never enable in production** — use migrations instead |
+| `KK_DB_SSL` | No | `false` | Enable SSL for database connections |
+
+### Redis (BullMQ Job Queue)
+
+| Variable | Required | Default | Description |
+|----------|----------|---------|-------------|
+| `KK_BULLMQ_HOST` | Yes | — | Redis hostname (e.g. `krakenkey-redis-dev` in Docker) |
+| `KK_BULLMQ_PORT` | No | `6379` | Redis port |
+| `KK_BULLMQ_PASSWORD` | No | — | Redis password (leave empty if no auth) |
+
+### DNS Provider
+
+Set `KK_DNS_PROVIDER` to select which provider handles DNS-01 ACME challenges. Only configure the credentials for your chosen provider.
+
+| Variable | Required | Default | Description |
+|----------|----------|---------|-------------|
+| `KK_DNS_PROVIDER` | No | `cloudflare` | DNS provider for ACME challenges (`cloudflare` or `route53`) |
+
+#### Cloudflare
+
+| Variable | Required | Default | Description |
+|----------|----------|---------|-------------|
+| `CLOUDFLARE_API_TOKEN` | Yes* | — | Cloudflare API token with DNS edit permissions for your zone |
+| `CLOUDFLARE_ACCOUNT_ID` | Yes* | — | Your Cloudflare account ID (found in dashboard URL) |
+| `CLOUDFLARE_ZONE_ID` | Yes* | — | Zone ID for the domain used in DNS-01 challenges (found on domain overview page) |
+
+*Required when `KK_DNS_PROVIDER=cloudflare`
+
+#### AWS Route 53
+
+| Variable | Required | Default | Description |
+|----------|----------|---------|-------------|
+| `AWS_ACCESS_KEY_ID` | Yes* | — | IAM access key with Route 53 permissions |
+| `AWS_SECRET_ACCESS_KEY` | Yes* | — | IAM secret key |
+| `AWS_REGION` | Yes* | `us-east-1` | AWS region |
+| `KK_AWS_ROUTE53_HOSTED_ZONE_ID` | Yes* | — | Route 53 hosted zone ID for the ACME challenge domain |
+
+*Required when `KK_DNS_PROVIDER=route53`
+
+### ACME / Let's Encrypt
+
+| Variable | Required | Default | Description |
+|----------|----------|---------|-------------|
+| `ACME_ACCOUNT_KEY` | Yes | — | RSA 4096-bit private key in PEM format for the ACME account. See [Generating an ACME Account Key](#generating-an-acme-account-key) |
+| `ACME_DIRECTORY_URL` | No | Let's Encrypt Staging | Custom ACME directory URL. Defaults to staging; set to `https://acme-v02.api.letsencrypt.org/directory` for production |
+
+### Authentication (Authentik OIDC)
+
+| Variable | Required | Default | Description |
+|----------|----------|---------|-------------|
+| `KK_AUTHENTIK_DOMAIN` | Yes | — | Authentik instance domain (e.g. `auth-dev.krakenkey.io`). Use single-level subdomain for Cloudflare proxy |
+| `KK_AUTHENTIK_ENROLLMENT_SLUG` | Yes | — | Authentik enrollment flow slug (e.g. `krakenkey`) |
+| `KK_AUTHENTIK_ISSUER_URL` | Yes | — | OIDC issuer URL (e.g. `https://auth-dev.krakenkey.io/application/o/krakenkey/`) |
+| `KK_AUTHENTIK_CLIENT_ID` | Yes | — | OAuth2 client ID from Authentik provider configuration |
+| `KK_AUTHENTIK_CLIENT_SECRET` | Yes | — | OAuth2 client secret from Authentik provider configuration |
+| `KK_AUTHENTIK_REDIRECT_URI` | Yes | — | OAuth2 callback URL pointing to your API (e.g. `https://api-dev.krakenkey.io/auth/callback`) |
+| `KK_AUTHENTIK_POST_ENROLLMENT_REDIRECT` | Yes | — | Where to redirect after user enrollment (e.g. `https://api-dev.krakenkey.io/auth/login`) |
+
+### API Key Hashing
+
+| Variable | Required | Default | Description |
+|----------|----------|---------|-------------|
+| `KK_HMAC_SECRET` | Yes | — | 32-byte hex secret used as salt for scrypt hashing of API keys. Generate with `openssl rand -hex 32` |
+
+### Billing (Stripe)
+
+| Variable | Required | Default | Description |
+|----------|----------|---------|-------------|
+| `KK_STRIPE_SECRET_KEY` | Yes* | — | Stripe secret API key |
+| `KK_STRIPE_WEBHOOK_SECRET` | Yes* | — | Stripe webhook signing secret for verifying webhook payloads |
+| `KK_STRIPE_PRICE_STARTER` | Yes* | — | Stripe Price ID for the Starter plan |
+| `KK_STRIPE_PRICE_TEAM` | Yes* | — | Stripe Price ID for the Team plan |
+| `KK_STRIPE_PRICE_BUSINESS` | Yes* | — | Stripe Price ID for the Business plan |
+| `KK_STRIPE_PRICE_ENTERPRISE` | Yes* | — | Stripe Price ID for the Enterprise plan |
+
+*Required only if billing features are enabled
+
+### Probe Service Keys
+
+| Variable | Required | Default | Description |
+|----------|----------|---------|-------------|
+| `KK_PROBE_API_KEY` | No | — | Service API key for probe authentication. Auto-seeded on startup if set. Used by external probe instances to authenticate with the API |
+
+---
+
+## Frontend Environment Variables
+
+| Variable | Required | Default | Description |
+|----------|----------|---------|-------------|
+| `KK_API_URL` | Yes | — | Full URL to the backend API (e.g. `https://dev-api.krakenkey.io`). Use single-level subdomain for Cloudflare Free tier |
+| `KK_ACME_AUTH_ZONE_DOMAIN` | Yes | — | Auth zone domain for ACME DNS-01 challenge delegation. Displayed in the UI for user instructions |
+| `VITE_ALLOWED_HOST` | No | — | Domain allowed by the Vite dev server for HMR/WebSocket connections |
+
+---
+
+## Setup Instructions
+
+### Generating an ACME Account Key
+
+```bash
+# Generate a 4096-bit RSA key
+openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -out acme-account.key
+
+# View the key (for copying into .env)
+cat acme-account.key
+```
+
+When placing the key in your `.env` file, wrap it in single quotes and replace newlines with literal `\n`:
+
+```env
+ACME_ACCOUNT_KEY=''
+```
+
+The backend automatically normalizes PEM formatting (handles literal `\n`, stray quotes, incorrect line wrapping).
+
+### Generating an HMAC Secret
+
+```bash
+openssl rand -hex 32
+```
+
+### Setting Up for Development
+
+1. Copy the example environment files:
+
+```bash
+cp backend/.env.example backend/.env
+cp frontend/.env.example frontend/.env
+```
+
+2. If using the devcontainer, the Docker Compose file provides PostgreSQL and Redis automatically. The default hostnames are:
+ - PostgreSQL: `krakenkey-db-dev:5432`
+ - Redis: `krakenkey-redis-dev:6379`
+
+3. For local development outside Docker, point `KK_DB_HOST` and `KK_BULLMQ_HOST` to `localhost`.
+
+### Production Checklist
+
+- [ ] Set `NODE_ENV=production`
+- [ ] Set `KK_DB_SYNCHRONIZE=false` (use migrations)
+- [ ] Set `KK_DB_LOGGING=false`
+- [ ] Set `KK_DB_SSL=true`
+- [ ] Use the Let's Encrypt production directory URL
+- [ ] Generate a strong `KK_HMAC_SECRET`
+- [ ] Configure Stripe with production keys
+- [ ] Ensure `KK_AUTHENTIK_REDIRECT_URI` points to your production API domain
+- [ ] Set `KK_APP_DOMAIN` to your production web domain
diff --git a/backend/docs/DATABASE.md b/backend/docs/DATABASE.md
index 0caeb1b..6d99620 100644
--- a/backend/docs/DATABASE.md
+++ b/backend/docs/DATABASE.md
@@ -2,336 +2,337 @@
## Overview
-The KrakenKey backend uses PostgreSQL as the primary data store via TypeORM ORM.
+KrakenKey uses PostgreSQL as the primary data store via TypeORM. Migrations run automatically on startup.
-## Database Configuration
+## Configuration
-### Connection Details
+Configured via environment variables (see [Configuration](./CONFIGURATION.md)):
-Configured via environment variables:
+| Variable | Description |
+|----------|-------------|
+| `KK_DB_HOST` | Database hostname |
+| `KK_DB_PORT` | Database port (default: 5432) |
+| `KK_DB_USERNAME` | Database user |
+| `KK_DB_PASSWORD` | Database password |
+| `KK_DB_DATABASE` | Database name (default: krakenkey) |
+| `KK_DB_SYNCHRONIZE` | Auto-sync schema — **never enable in production** |
+| `KK_DB_SSL` | Enable SSL connections |
-```env
-TYPEORM_HOST=localhost # Database host
-TYPEORM_PORT=5432 # Database port
-TYPEORM_USERNAME=postgres # Database user
-TYPEORM_PASSWORD=password # Database password
-TYPEORM_DATABASE=krakenkey # Database name
-TYPEORM_SSL=false # Enable SSL
-TYPEORM_SYNCHRONIZE=false # Auto-sync schema (dev only)
-```
+---
-### Connection Pool
+## Entities
-- Managed by TypeORM
-- Connection pooling enabled by default
-- Maximum connections: configurable via TypeORM options
+### User
-### Initialization
+Represents an authenticated user. The `id` comes from Authentik's `sub` claim (not a UUID).
-```bash
-# Create database and tables
-yarn run db:create
+```sql
+CREATE TABLE "user" (
+ "id" TEXT PRIMARY KEY, -- Authentik sub claim
+ "username" VARCHAR NOT NULL UNIQUE,
+ "email" VARCHAR NOT NULL UNIQUE,
+ "groups" TEXT[] DEFAULT '{}', -- Authentik groups
+ "displayName" VARCHAR,
+ "notificationPreferences" JSONB,
+ "createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ "autoRenewalConfirmedAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ "autoRenewalReminderSentAt" TIMESTAMP,
+ "role" VARCHAR, -- org role: owner/admin/member/viewer
+ "organizationId" UUID REFERENCES organization(id) ON DELETE CASCADE
+);
```
-**Script**: `scripts/create-db.ts`
+**Relationships:**
+- Has many `UserApiKey`
+- Has many `Domain`
+- Has many `TlsCrt`
+- Belongs to `Organization` (optional)
---
-## Entity: TlsCrt
+### Domain
-Represents a TLS certificate request and its associated data.
-
-### Schema
+Represents a domain registered by a user for DNS verification. Verification is required before certificates can be issued.
```sql
-CREATE TABLE "tls_crt" (
- "id" SERIAL PRIMARY KEY,
- "rawCsr" VARCHAR NOT NULL,
- "parsedCsr" JSONB NOT NULL,
- "crtPem" TEXT,
- "status" VARCHAR DEFAULT 'pending',
+CREATE TABLE "domain" (
+ "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
+ "hostname" VARCHAR NOT NULL, -- FQDN (max 253 chars)
+ "verificationCode" VARCHAR NOT NULL, -- DNS TXT value (hidden from API)
+ "isVerified" BOOLEAN DEFAULT FALSE,
+ "userId" TEXT NOT NULL REFERENCES "user"(id),
"createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
- "updatedAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+ "updatedAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ UNIQUE("userId", "hostname")
);
+
+CREATE INDEX idx_domain_user_verified ON domain("userId", "isVerified");
```
-### TypeScript Definition
+**Relationships:**
+- Belongs to `User`
-```typescript
-import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
+**Verification:**
+- A daily cron job at 02:00 UTC re-checks DNS TXT records
+- If the record is missing, `isVerified` is set to `false` and an email notification is sent
+- Parent domain verification covers subdomains (verifying `example.com` authorizes `sub.example.com`)
-@Entity()
-export class TlsCrt {
- @PrimaryGeneratedColumn()
- id: number;
+---
- @Column()
- rawCsr: string;
+### TlsCrt
- @Column('jsonb')
- parsedCsr: JSON;
+Represents a TLS certificate request and its lifecycle from submission through issuance, renewal, and revocation.
- @Column({ type: 'text', nullable: true })
- crtPem: string | null;
+```sql
+CREATE TABLE "tls_crt" (
+ "id" SERIAL PRIMARY KEY,
+ "rawCsr" VARCHAR NOT NULL, -- Original CSR PEM (hidden from API)
+ "parsedCsr" JSONB NOT NULL, -- Parsed CSR metadata
+ "crtPem" TEXT, -- Issued certificate PEM
+ "status" VARCHAR DEFAULT 'pending', -- pending/issuing/issued/failed/renewing/revoking/revoked
+ "expiresAt" TIMESTAMP, -- Certificate expiration
+ "lastRenewedAt" TIMESTAMP,
+ "autoRenew" BOOLEAN DEFAULT TRUE,
+ "renewalCount" INTEGER DEFAULT 0,
+ "lastRenewalAttemptAt" TIMESTAMP,
+ "revocationReason" INTEGER, -- RFC 5280 reason code (0-10)
+ "revokedAt" TIMESTAMP,
+ "createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ "userId" TEXT NOT NULL REFERENCES "user"(id)
+);
- @Column({ default: 'pending', nullable: true })
- status: string;
-}
+CREATE INDEX idx_tls_crt_user ON tls_crt("userId");
+CREATE INDEX idx_tls_crt_renewal ON tls_crt("status", "autoRenew", "expiresAt");
```
-### Column Definitions
-
-| Column | Type | Required | Nullable | Default | Description |
-|--------|------|----------|----------|---------|-------------|
-| `id` | INTEGER | Yes | No | AUTO | Primary key, auto-incremented |
-| `rawCsr` | VARCHAR | Yes | No | - | Original CSR in PEM format |
-| `parsedCsr` | JSONB | Yes | No | - | Parsed CSR structure |
-| `crtPem` | TEXT | No | Yes | NULL | Issued certificate in PEM format |
-| `status` | VARCHAR | No | Yes | 'pending' | Certificate status |
-
-### Status Values
+**Status Values:**
-| Status | Meaning | Description |
-|--------|---------|-------------|
-| `pending` | Awaiting Processing | CSR received, job queued |
-| `issuing` | In Progress | ACME workflow executing |
-| `issued` | Success | Certificate successfully issued and stored |
-| `failed` | Error | Issuance failed after max retries |
+| Status | Description |
+|--------|-------------|
+| `pending` | CSR received, validated, job queued |
+| `issuing` | ACME workflow actively running |
+| `issued` | Certificate successfully issued |
+| `failed` | Issuance failed after 3 retries |
+| `renewing` | Renewal in progress |
+| `revoking` | Revocation request sent to ACME CA |
+| `revoked` | Certificate successfully revoked |
-### parsedCsr Structure
-
-JSON representation of the parsed CSR:
+**parsedCsr Structure:**
```json
{
"subject": [
- {
- "name": "countryName",
- "shortName": "C",
- "value": "US"
- },
- {
- "name": "commonName",
- "shortName": "CN",
- "value": "example.com"
- }
- ],
- "attributes": [
- {
- "name": "extensionRequest",
- "value": {...}
- }
+ {"name": "commonName", "shortName": "CN", "value": "example.com"}
],
"extensions": [
{
"name": "subjectAltName",
"altNames": [
- {
- "type": 2,
- "value": "example.com"
- },
- {
- "type": 2,
- "value": "www.example.com"
- }
+ {"type": 2, "value": "example.com"},
+ {"type": 2, "value": "www.example.com"}
]
}
],
- "publicKeyLength": 2048
+ "publicKeyLength": 4096
}
```
-### Data Size Estimates
-
-**Average Record Size**:
-- `rawCsr`: 1-2 KB
-- `parsedCsr`: 2-3 KB
-- `crtPem`: 2-3 KB
-- **Total per record**: 5-8 KB
-
-**Storage for 100,000 records**: ~500 MB - 1 GB
+**Relationships:**
+- Belongs to `User`
---
-## Indexes
+### UserApiKey
-### Current Indexes
+API keys for programmatic access. Keys are hashed with scrypt using `KK_HMAC_SECRET` as salt.
```sql
--- Primary key index (auto-created)
-CREATE INDEX idx_tls_crt_pkey ON tls_crt(id);
+CREATE TABLE "user_api_key" (
+ "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
+ "name" VARCHAR NOT NULL DEFAULT 'default', -- Friendly name (max 100 chars)
+ "hash" VARCHAR NOT NULL UNIQUE, -- scrypt hash (hidden from API)
+ "expiresAt" TIMESTAMP,
+ "createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ "userId" TEXT NOT NULL REFERENCES "user"(id) ON DELETE CASCADE
+);
--- Status index (recommended for queries)
-CREATE INDEX idx_tls_crt_status ON tls_crt(status);
+CREATE INDEX idx_api_key_user ON user_api_key("userId");
```
-### Recommended Indexes
+Key format: `kk_` prefix followed by random bytes. The raw key is only returned once at creation time.
-```sql
--- For status-based queries
-CREATE INDEX idx_tls_crt_status ON tls_crt(status);
+---
--- For time-range queries
-CREATE INDEX idx_tls_crt_created_at ON tls_crt(createdAt);
-CREATE INDEX idx_tls_crt_updated_at ON tls_crt(updatedAt);
+### ServiceApiKey
--- Composite index for common queries
-CREATE INDEX idx_tls_crt_status_created ON tls_crt(status, createdAt DESC);
+System-level API keys for probe instances and internal services. Separate from user keys.
+
+```sql
+CREATE TABLE "service_api_key" (
+ "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
+ "name" VARCHAR NOT NULL,
+ "hash" VARCHAR NOT NULL UNIQUE, -- scrypt hash (hidden from API)
+ "expiresAt" TIMESTAMP,
+ "revokedAt" TIMESTAMP,
+ "createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
```
----
+Key format: `kk_svc_` prefix. Can be revoked (soft delete via `revokedAt`).
-## Queries
+---
-### Find by ID
+### Subscription
-```typescript
-const tlsCrt = await this.TlsCrtRepository.findOneBy({ id });
-```
+Stripe subscription records linked to users or organizations.
-**SQL**:
```sql
-SELECT * FROM tls_crt WHERE id = $1;
+CREATE TABLE "subscription" (
+ "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
+ "plan" VARCHAR NOT NULL, -- starter/team/business/enterprise
+ "status" VARCHAR NOT NULL, -- active/past_due/canceled/incomplete/trialing
+ "stripeCustomerId" VARCHAR,
+ "stripeSubscriptionId" VARCHAR UNIQUE,
+ "currentPeriodStart" TIMESTAMP,
+ "currentPeriodEnd" TIMESTAMP,
+ "userId" TEXT REFERENCES "user"(id),
+ "organizationId" UUID REFERENCES organization(id),
+ "createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
+ "updatedAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE INDEX idx_subscription_user ON subscription("userId");
+CREATE INDEX idx_subscription_org ON subscription("organizationId");
```
-### Find by Status
+**Relationships:**
+- Belongs to `User` (personal subscription) OR `Organization` (team subscription)
-```typescript
-const pending = await this.TlsCrtRepository.find({
- where: { status: 'pending' }
-});
-```
+---
+
+### Organization
+
+Teams with role-based access control. Requires Team+ plan.
-**SQL**:
```sql
-SELECT * FROM tls_crt WHERE status = $1;
+CREATE TABLE "organization" (
+ "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
+ "name" VARCHAR NOT NULL, -- 2-80 characters
+ "ownerId" TEXT NOT NULL,
+ "status" VARCHAR DEFAULT 'active', -- active/dissolving
+ "createdAt" TIMESTAMP DEFAULT CURRENT_TIMESTAMP
+);
+
+CREATE INDEX idx_org_owner ON organization("ownerId");
```
-### Count by Status
+**Relationships:**
+- Has one owner (`User`)
+- Has many members (`User` via `organizationId` foreign key)
+- Has one `Subscription`
-```typescript
-const count = await this.TlsCrtRepository.count({
- where: { status: 'issued' }
-});
-```
+---
-**SQL**:
-```sql
-SELECT COUNT(*) FROM tls_crt WHERE status = $1;
-```
+### Endpoint
-### Update Status
+TLS endpoints monitored by probes.
-```typescript
-await this.TlsCrtRepository.update(
- { id },
- { status: 'issued', crtPem: certificate }
+```sql
+CREATE TABLE "endpoint" (
+ "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
+ "host" VARCHAR NOT NULL,
+ "port" INTEGER NOT NULL,
+ "sni" VARCHAR, -- SNI override
+ "label" VARCHAR, -- Friendly name
+ "isActive" BOOLEAN DEFAULT TRUE,
+ "lastScanRequestedAt" TIMESTAMP,
+ "ownerId" TEXT NOT NULL REFERENCES "user"(id)
);
```
-**SQL**:
-```sql
-UPDATE tls_crt
-SET status = $1, crtPem = $2, updatedAt = CURRENT_TIMESTAMP
-WHERE id = $3;
-```
+**Relationships:**
+- Belongs to `User`
+- Has many `EndpointHostedRegion`
+- Has many `EndpointProbeAssignment`
-### Recent Certificates (Issued)
+---
-```typescript
-const recent = await this.TlsCrtRepository.find({
- where: { status: 'issued' },
- order: { id: 'DESC' },
- take: 10
-});
-```
+### EndpointHostedRegion
+
+Join table linking endpoints to managed cloud probe regions.
-**SQL**:
```sql
-SELECT * FROM tls_crt
-WHERE status = 'issued'
-ORDER BY id DESC
-LIMIT 10;
+CREATE TABLE "endpoint_hosted_region" (
+ "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
+ "endpointId" UUID NOT NULL REFERENCES endpoint(id),
+ "region" VARCHAR NOT NULL,
+ UNIQUE("endpointId", "region")
+);
```
---
-## Relationships
+### EndpointProbeAssignment
-**Current**: No relationships defined
+Join table linking endpoints to connected probe instances.
-**Potential Future Relationships**:
+```sql
+CREATE TABLE "endpoint_probe_assignment" (
+ "id" UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
+ "endpointId" UUID NOT NULL REFERENCES endpoint(id),
+ "probeId" UUID NOT NULL,
+ UNIQUE("endpointId", "probeId")
+);
+```
-```typescript
-// Future: User/Account relationship
-@ManyToOne(() => User)
-@JoinColumn({ name: 'userId' })
-user: User;
+---
-// Future: Certificate renewal chain
-@OneToMany(() => TlsCrt, (crt) => crt.previous)
-renewals: TlsCrt[];
+## Entity Relationship Diagram
-@ManyToOne(() => TlsCrt, (crt) => crt.renewals, { nullable: true })
-@JoinColumn({ name: 'previousCrtId' })
-previous: TlsCrt | null;
+```
+┌──────────┐ ┌──────────────┐ ┌───────────────┐
+│ User │──1:N──│ Domain │ │ Organization │
+│ │──1:N──│ TlsCrt │ │ │
+│ │──1:N──│ UserApiKey │ └───────┬───────┘
+│ │──N:1──│ Organization │──1:1──Subscription
+│ │──1:N──│ Endpoint │
+└──────────┘ └──────────────┘
+ │
+ ┌──────┴──────┐
+ │ │
+ EndpointHosted EndpointProbe
+ Region Assignment
```
---
## Migrations
-TypeORM synchronization can be enabled for development:
-
-```typescript
-// In app.module.ts
-synchronize: configService.get('TYPEORM_SYNCHRONIZE') === 'true'
-```
+Migrations run automatically on startup via TypeORM's `migrationsRun: true` configuration.
### Generating Migrations
```bash
-# Generate migration from schema changes
-npx typeorm migration:generate src/migrations/AddColumnX
+# Generate migration from entity changes
+npx typeorm migration:generate src/migrations/MigrationName
+
+# Create empty migration
+npx typeorm migration:create src/migrations/MigrationName
-# Run migrations
+# Run pending migrations
npx typeorm migration:run
-# Revert migration
+# Revert last migration
npx typeorm migration:revert
```
-### Manual Migration Example
-
-```typescript
-// src/migrations/1234567890000-AddExpiryDateToTlsCrt.ts
-import { MigrationInterface, QueryRunner, TableColumn } from 'typeorm';
-
-export class AddExpiryDateToTlsCrt1234567890000
- implements MigrationInterface
-{
- public async up(queryRunner: QueryRunner): Promise {
- await queryRunner.addColumn(
- 'tls_crt',
- new TableColumn({
- name: 'expiresAt',
- type: 'TIMESTAMP',
- isNullable: true,
- }),
- );
- }
-
- public async down(queryRunner: QueryRunner): Promise {
- await queryRunner.dropColumn('tls_crt', 'expiresAt');
- }
-}
-```
+**Important:** In production, always use migrations (`KK_DB_SYNCHRONIZE=false`). The `synchronize` option is for development convenience only and can cause data loss.
---
## Backup & Recovery
-### Backup Strategies
+### Backup
```bash
# Full database backup
@@ -340,11 +341,11 @@ pg_dump -h localhost -U postgres -d krakenkey > backup.sql
# Compressed backup
pg_dump -h localhost -U postgres -d krakenkey | gzip > backup.sql.gz
-# Custom format backup (faster restore)
+# Custom format (faster restore)
pg_dump -h localhost -U postgres -d krakenkey -F custom > backup.dump
```
-### Restore Procedures
+### Restore
```bash
# From SQL dump
@@ -357,302 +358,24 @@ gunzip -c backup.sql.gz | psql -h localhost -U postgres -d krakenkey
pg_restore -h localhost -U postgres -d krakenkey backup.dump
```
-### Point-in-Time Recovery
-
-Requires WAL (Write-Ahead Logging) enabled:
-
-```sql
--- Enable WAL
-ALTER SYSTEM SET wal_level = replica;
--- Restart PostgreSQL
-```
-
----
-
-## Maintenance
-
-### Vacuum & Analyze
-
-```bash
-# Clean up dead tuples
-VACUUM ANALYZE tls_crt;
-
-# Or via command line
-vacuumdb -h localhost -U postgres -d krakenkey -v -z tls_crt
-```
-
-### Check Bloat
-
-```sql
-SELECT
- schemaname,
- tablename,
- pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename)) as size
-FROM pg_tables
-WHERE schemaname NOT IN ('pg_catalog', 'information_schema')
-ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC;
-```
-
-### Reindex
-
-```bash
-# Reindex table
-REINDEX TABLE tls_crt;
-
-# Or via command line
-reindexdb -h localhost -U postgres -d krakenkey -t tls_crt
-```
-
---
## Monitoring
-### Connection Monitoring
-
```sql
-- Active connections
SELECT datname, count(*) FROM pg_stat_activity GROUP BY datname;
--- Long-running queries
-SELECT pid, now() - query_start, query
-FROM pg_stat_activity
-WHERE query_start < now() - interval '5 minutes';
-```
-
-### Disk Usage
-
-```sql
-- Database size
SELECT pg_size_pretty(pg_database_size('krakenkey'));
--- Table size
-SELECT
- schemaname,
- tablename,
- pg_size_pretty(pg_total_relation_size(schemaname||'.'||tablename))
-FROM pg_tables
-ORDER BY pg_total_relation_size(schemaname||'.'||tablename) DESC;
-```
-
-### Query Performance
-
-```sql
--- Enable query logging
-SET log_min_duration_statement = 1000; -- Log queries > 1 second
-
--- View slow query log
-SELECT query, calls, total_time, mean_time
-FROM pg_stat_statements
-ORDER BY mean_time DESC
-LIMIT 10;
-```
-
----
-
-## Data Privacy & Compliance
-
-### Data Sensitivity
-
-| Field | Sensitivity | Notes |
-|-------|-------------|-------|
-| `rawCsr` | Medium | Contains domain names, public key |
-| `parsedCsr` | Medium | Parsed certificate details |
-| `crtPem` | Low | Public certificate |
-| `status` | Low | Processing status |
-
-### Retention Policies
-
-**Recommended**:
-- Keep issued certificates: Until expiry + 1 year
-- Keep failed attempts: 90 days
-- Archive old records: After 5 years
-
-```typescript
-// Example: Clean old failed records
-async cleanupOldFailures(daysOld: number) {
- const cutoffDate = new Date();
- cutoffDate.setDate(cutoffDate.getDate() - daysOld);
-
- await this.TlsCrtRepository.delete({
- status: 'failed',
- createdAt: LessThan(cutoffDate),
- });
-}
-```
-
-### GDPR Considerations
-
-- No personal data stored directly (CSR/cert only contain domain names)
-- No user tracking without explicit user table
-- Implement data deletion on request
-- Log access to sensitive operations
-
----
-
-## Performance Tuning
-
-### Query Optimization
-
-```typescript
-// Bad: Fetches all columns
-const record = await this.TlsCrtRepository.findOneBy({ id });
-
-// Good: Select specific columns
-const record = await this.TlsCrtRepository.find({
- select: { id: true, status: true },
- where: { id },
- take: 1,
-});
-```
-
-### Pagination
-
-```typescript
-// Efficient: Paginated results
-const page = 1;
-const pageSize = 50;
-
-const records = await this.TlsCrtRepository.find({
- skip: (page - 1) * pageSize,
- take: pageSize,
- order: { id: 'DESC' },
-});
-```
+-- Table sizes
+SELECT tablename, pg_size_pretty(pg_total_relation_size('public.' || tablename))
+FROM pg_tables WHERE schemaname = 'public'
+ORDER BY pg_total_relation_size('public.' || tablename) DESC;
-### Caching
-
-```typescript
-// Cache frequently accessed records
-private cache = new Map();
-
-async findOneWithCache(id: number) {
- if (this.cache.has(id)) {
- return this.cache.get(id);
- }
- const record = await this.TlsCrtRepository.findOneBy({ id });
- if (record) {
- this.cache.set(id, record);
- }
- return record;
-}
-```
-
----
-
-## Troubleshooting
-
-### Connection Issues
-
-```typescript
-// Error: "connect ECONNREFUSED 127.0.0.1:5432"
-// Solution: Ensure PostgreSQL is running and listening on correct port
-// Check: TYPEORM_HOST, TYPEORM_PORT, TYPEORM_USERNAME, TYPEORM_PASSWORD
-
-// Error: "password authentication failed"
-// Solution: Verify credentials in environment variables
-```
-
-### Synchronization Issues
-
-```typescript
-// Error: "migration error"
-// Solution: Check TYPEORM_SYNCHRONIZE is 'false' in production
-// Run migrations manually instead
-
-// Error: "column does not exist"
-// Solution: Ensure migrations have been run
-// Run: yarn run typeorm migration:run
-```
-
-### Performance Issues
-
-```typescript
-// Slow queries
-// Solution: Add indexes on frequently queried columns
-// Use EXPLAIN ANALYZE to identify bottlenecks
-
-// High memory usage
-// Solution: Use pagination instead of loading all records
-// Implement query result streaming for large datasets
-```
-
----
-
-## Future Schema Enhancements
-
-### Proposed Additions
-
-```typescript
-// Track certificate lifecycle
-@CreateDateColumn()
-createdAt: Date;
-
-@UpdateDateColumn()
-updatedAt: Date;
-
-@Column({ type: 'timestamp', nullable: true })
-expiresAt: Date; // Certificate expiry date
-
-@Column({ type: 'timestamp', nullable: true })
-issuedAt: Date; // When certificate was issued
-
-
-// Track user/API key ownership
-@Column({ nullable: true })
-userId: string; // If user system added
-
-@Column({ nullable: true })
-apiKeyId: string; // If API key system added
-
-
-// Audit trail
-@Column('jsonb', { default: '[]' })
-statusHistory: Array<{
- status: string;
- timestamp: Date;
- reason?: string;
-}>;
-
-
-// Links to related certificates
-@Column({ nullable: true })
-renewalOf: number; // ID of certificate being renewed
-
-@Column({ default: false })
-autoRenewal: boolean; // Enable auto-renewal
-
-
-// Certificate details extracted from issued cert
-@Column({ nullable: true })
-serialNumber: string;
-
-@Column({ type: 'text', nullable: true })
-issuer: string;
-
-@Column({ type: 'jsonb', nullable: true })
-extensions: JSON; // Certificate extensions
-```
-
-### Migration Script
-
-```typescript
-// src/migrations/AddEnhancedFields.ts
-export class AddEnhancedFields1234567890000 implements MigrationInterface {
- public async up(queryRunner: QueryRunner): Promise {
- await queryRunner.addColumn(
- 'tls_crt',
- new TableColumn({
- name: 'createdAt',
- type: 'timestamp',
- default: 'CURRENT_TIMESTAMP',
- }),
- );
- // ... add other columns
- }
-
- public async down(queryRunner: QueryRunner): Promise {
- await queryRunner.dropColumn('tls_crt', 'createdAt');
- // ... drop other columns
- }
-}
+-- Long-running queries
+SELECT pid, now() - query_start AS duration, query
+FROM pg_stat_activity
+WHERE query_start < now() - interval '5 minutes';
```
diff --git a/backend/docs/ENDPOINTS.md b/backend/docs/ENDPOINTS.md
new file mode 100644
index 0000000..62b5324
--- /dev/null
+++ b/backend/docs/ENDPOINTS.md
@@ -0,0 +1,230 @@
+# Endpoint Monitoring
+
+KrakenKey can monitor TLS endpoints (servers) to track certificate expiry, trust status, and connection health. Endpoints are scanned by probes — either managed cloud probes or your own connected probe instances.
+
+## Concepts
+
+| Term | Description |
+|------|-------------|
+| **Endpoint** | A hostname + port combination to monitor (e.g. `api.example.com:443`) |
+| **Probe** | A service that connects to endpoints and reports TLS scan results |
+| **Managed (Hosted) Probe** | Cloud-hosted probe in a specific region, provided by KrakenKey |
+| **Connected Probe** | Your own self-hosted probe instance, registered via service API key |
+| **Scan** | A single TLS connection + certificate inspection by a probe |
+| **Scan Result** | Certificate details, expiry, trust chain, connection latency from a scan |
+
+## Plan Limits
+
+| Feature | Free | Starter | Team | Business | Enterprise |
+|---------|------|---------|------|----------|------------|
+| Endpoints | 3 | 10 | 25 | 75 | Unlimited |
+| Hosted Probe Regions | — | — | 5 | 15 | Unlimited |
+| Hosted Endpoints | — | — | — | 100 | Unlimited |
+| Scan Interval | 60 min | 30 min | 5 min | 1 min | Custom |
+
+## API Endpoints
+
+### Create Endpoint
+
+```
+POST /endpoints
+```
+
+**Request:**
+```json
+{
+ "host": "api.example.com",
+ "port": 443,
+ "sni": "api.example.com",
+ "label": "Production API",
+ "probeIds": ["probe-uuid-1"],
+ "hostedRegions": ["us-east-1"]
+}
+```
+
+| Field | Required | Default | Description |
+|-------|----------|---------|-------------|
+| `host` | Yes | — | Hostname or IP to connect to |
+| `port` | Yes | — | Port number (typically 443) |
+| `sni` | No | — | Server Name Indication override |
+| `label` | No | — | Friendly name for the endpoint |
+| `probeIds` | No | `[]` | Connected probe IDs to assign |
+| `hostedRegions` | No | `[]` | Managed probe regions to enable |
+
+### List Endpoints
+
+```
+GET /endpoints
+```
+
+Returns all endpoints owned by the user (or organization).
+
+### Get Endpoint
+
+```
+GET /endpoints/:id
+```
+
+### Update Endpoint
+
+```
+PATCH /endpoints/:id
+```
+
+**Request:**
+```json
+{
+ "label": "Updated Label",
+ "sni": "new-sni.example.com",
+ "isActive": false
+}
+```
+
+### Delete Endpoint
+
+```
+DELETE /endpoints/:id
+```
+
+### Request Scan
+
+Trigger an immediate scan for an endpoint across all assigned probes.
+
+```
+POST /endpoints/:id/scan
+```
+
+After requesting a scan, poll the results endpoint. The frontend auto-polls every 10 seconds for up to 60 seconds.
+
+### Get Scan Results
+
+```
+GET /endpoints/:id/results
+```
+
+Returns paginated scan results from all assigned probes.
+
+### Get Latest Results
+
+```
+GET /endpoints/:id/results/latest
+```
+
+Returns the most recent scan result from each assigned probe.
+
+### Export Results
+
+```
+GET /endpoints/:id/results/export?format=csv
+GET /endpoints/:id/results/export?format=json
+```
+
+Download scan results as CSV or JSON.
+
+## Probe Management
+
+### List Your Probes
+
+```
+GET /endpoints/probes/mine
+```
+
+Returns all connected probes available to the user.
+
+### Assign Probes
+
+```
+POST /endpoints/:id/probes
+```
+
+**Request:**
+```json
+{
+ "probeIds": ["probe-uuid-1", "probe-uuid-2"]
+}
+```
+
+### Unassign Probe
+
+```
+DELETE /endpoints/:id/probes/:probeId
+```
+
+## Hosted Region Management
+
+### Add Hosted Region
+
+```
+POST /endpoints/:id/regions
+```
+
+**Request:**
+```json
+{
+ "region": "us-east-1"
+}
+```
+
+Subject to plan-based hosted region limits.
+
+### Remove Hosted Region
+
+```
+DELETE /endpoints/:id/regions/:region
+```
+
+## Scan Results
+
+Each scan result includes:
+
+| Field | Description |
+|-------|-------------|
+| Certificate chain | Full certificate chain from the server |
+| Expiry date | Certificate expiration timestamp |
+| Trust status | Whether the certificate is trusted by the probe's root store |
+| Connection success | Whether the TLS handshake completed |
+| Latency | Connection time in milliseconds |
+| Probe ID | Which probe performed the scan |
+| Scanned at | Timestamp of the scan |
+
+### Expiry Indicators
+
+The UI displays color-coded badges:
+- **Red** (danger): Expires within 7 days
+- **Yellow** (warning): Expires within 30 days
+- **Green** (success): More than 30 days until expiry
+
+## Endpoint Entity Schema
+
+| Column | Type | Description |
+|--------|------|-------------|
+| `id` | UUID | Primary key |
+| `host` | varchar | Hostname or IP |
+| `port` | integer | Port number |
+| `sni` | varchar | SNI override (nullable) |
+| `label` | varchar | Friendly name (nullable) |
+| `isActive` | boolean | Whether scanning is enabled (default: true) |
+| `lastScanRequestedAt` | timestamp | Last manual scan request (nullable) |
+| `ownerId` | varchar | Owner user ID |
+
+### Related Entities
+
+**EndpointHostedRegion** — join table for managed probe regions:
+
+| Column | Type | Description |
+|--------|------|-------------|
+| `id` | UUID | Primary key |
+| `endpointId` | UUID | Endpoint reference |
+| `region` | varchar | Cloud region identifier |
+
+Unique constraint on `(endpointId, region)`.
+
+**EndpointProbeAssignment** — join table for connected probes:
+
+| Column | Type | Description |
+|--------|------|-------------|
+| `id` | UUID | Primary key |
+| `endpointId` | UUID | Endpoint reference |
+| `probeId` | UUID | Probe reference |
+
+Unique constraint on `(endpointId, probeId)`.
diff --git a/backend/docs/INTEGRATIONS.md b/backend/docs/INTEGRATIONS.md
new file mode 100644
index 0000000..cdccaa7
--- /dev/null
+++ b/backend/docs/INTEGRATIONS.md
@@ -0,0 +1,290 @@
+# External Integrations
+
+KrakenKey integrates with several external services for authentication, certificate issuance, DNS management, and billing. This guide covers how each integration works and how to configure it.
+
+## Let's Encrypt (ACME)
+
+### Overview
+
+KrakenKey uses the ACME protocol to automate certificate issuance with Let's Encrypt. The backend acts as an ACME client, creating orders, solving DNS-01 challenges, and retrieving signed certificates.
+
+### How It Works
+
+1. Backend creates an ACME order for the domains in a CSR
+2. Let's Encrypt returns DNS-01 challenges for each domain
+3. Backend creates TXT records via the configured DNS provider
+4. Backend notifies Let's Encrypt that challenges are ready
+5. Let's Encrypt verifies the TXT records and issues the certificate
+6. Backend retrieves the certificate and cleans up TXT records
+
+### Configuration
+
+| Variable | Description |
+|----------|-------------|
+| `ACME_ACCOUNT_KEY` | 4096-bit RSA private key in PEM format |
+| `ACME_CONTACT_EMAIL` | Contact email registered with Let's Encrypt |
+| `ACME_DIRECTORY_URL` | ACME directory (defaults to Let's Encrypt Staging) |
+| `ACME_AUTH_ZONE_DOMAIN` | DNS zone for challenge delegation |
+
+### Environments
+
+| Environment | Directory URL | Rate Limits |
+|-------------|--------------|-------------|
+| **Staging** (default) | `https://acme-staging-v02.api.letsencrypt.org/directory` | Generous limits, issues untrusted test certificates |
+| **Production** | `https://acme-v02.api.letsencrypt.org/directory` | 50 certs/domain/week, 300 new orders/account/3hrs |
+
+### Setting Up an ACME Account
+
+The ACME account is created automatically on first use. You only need to generate and configure the account key:
+
+```bash
+# Generate account key
+openssl genpkey -algorithm RSA -pkeyopt rsa_keygen_bits:4096 -out acme-account.key
+
+# Copy to .env (replace newlines with \n)
+echo "ACME_ACCOUNT_KEY='$(cat acme-account.key | tr '\n' '~' | sed 's/~/\\n/g')'"
+```
+
+### Switching to Production
+
+When you're ready to issue real certificates:
+
+1. Set `ACME_DIRECTORY_URL=https://acme-v02.api.letsencrypt.org/directory`
+2. Optionally generate a new account key for production (recommended)
+3. Ensure your DNS provider is correctly configured
+4. Test with a single domain before bulk issuance
+
+---
+
+## Cloudflare DNS
+
+### Overview
+
+Cloudflare is the default DNS provider for solving ACME DNS-01 challenges. KrakenKey creates and deletes TXT records in your Cloudflare zone automatically during certificate issuance.
+
+### How It Works
+
+- TXT records are created at `_acme-challenge.{domain}` within your configured zone
+- Dots in hostnames are flattened to dashes (e.g. `sub.example.com` → `_acme-challenge-sub-example-com`)
+- Records use a 60-second TTL for fast propagation
+- Records are automatically cleaned up after challenge completion
+
+### Configuration
+
+| Variable | Description |
+|----------|-------------|
+| `CLOUDFLARE_API_TOKEN` | API token with DNS edit permission |
+| `CLOUDFLARE_ACCOUNT_ID` | Your Cloudflare account ID |
+| `CLOUDFLARE_ZONE_ID` | Zone ID for the ACME challenge domain |
+
+### Creating a Cloudflare API Token
+
+1. Go to [Cloudflare Dashboard](https://dash.cloudflare.com) → My Profile → API Tokens
+2. Click **Create Token**
+3. Use the **Edit zone DNS** template, or create a custom token with:
+ - **Permissions**: Zone → DNS → Edit
+ - **Zone Resources**: Include → Specific zone → your ACME zone
+4. Copy the token to `CLOUDFLARE_API_TOKEN`
+
+### Finding Your Zone ID and Account ID
+
+1. Go to [Cloudflare Dashboard](https://dash.cloudflare.com)
+2. Select the domain you're using for ACME challenges
+3. On the **Overview** page, scroll down to the right sidebar
+4. Copy the **Zone ID** and **Account ID**
+
+---
+
+## AWS Route 53
+
+### Overview
+
+Route 53 is an alternative DNS provider for ACME DNS-01 challenges. Select it by setting `KK_DNS_PROVIDER=route53`.
+
+### How It Works
+
+- TXT records are created via the Route 53 `ChangeResourceRecordSets` API
+- Uses UPSERT action (creates or updates existing records)
+- Dots in hostnames are flattened to dashes
+- Record values are wrapped in quotes (Route 53 requirement for TXT records)
+- 60-second TTL
+
+### Configuration
+
+| Variable | Description |
+|----------|-------------|
+| `KK_DNS_PROVIDER` | Set to `route53` |
+| `AWS_ACCESS_KEY_ID` | IAM access key |
+| `AWS_SECRET_ACCESS_KEY` | IAM secret key |
+| `AWS_REGION` | AWS region (e.g. `us-east-1`) |
+| `KK_AWS_ROUTE53_HOSTED_ZONE_ID` | Hosted zone ID for the ACME challenge domain |
+
+### IAM Policy
+
+The IAM user or role needs the following minimum permissions:
+
+```json
+{
+ "Version": "2012-10-17",
+ "Statement": [
+ {
+ "Effect": "Allow",
+ "Action": [
+ "route53:ChangeResourceRecordSets",
+ "route53:ListResourceRecordSets"
+ ],
+ "Resource": "arn:aws:route53:::hostedzone/YOUR_HOSTED_ZONE_ID"
+ }
+ ]
+}
+```
+
+---
+
+## Authentik (OIDC Authentication)
+
+### Overview
+
+KrakenKey uses Authentik as its identity provider via OpenID Connect (OIDC). Authentik handles user registration, login, and session management. The backend exchanges OIDC authorization codes for JWT tokens and provisions users on first login.
+
+### Authentication Flow
+
+```
+User ──▶ KrakenKey Frontend ──▶ Authentik Login Page
+ │
+ User authenticates
+ │
+ ▼
+Authentik ──▶ Redirect to /auth/callback with authorization code
+ │
+ Backend exchanges code
+ for access + ID tokens
+ │
+ ▼
+ Backend extracts claims,
+ provisions user (JIT),
+ returns JWT session
+```
+
+### Configuration
+
+| Variable | Description |
+|----------|-------------|
+| `KK_AUTHENTIK_DOMAIN` | Authentik instance domain |
+| `KK_AUTHENTIK_ENROLLMENT_SLUG` | Enrollment flow slug |
+| `KK_AUTHENTIK_ISSUER_URL` | OIDC issuer URL (must match token `iss` claim) |
+| `KK_AUTHENTIK_CLIENT_ID` | OAuth2 client ID |
+| `KK_AUTHENTIK_CLIENT_SECRET` | OAuth2 client secret |
+| `KK_AUTHENTIK_REDIRECT_URI` | Callback URL (e.g. `https://api.example.com/auth/callback`) |
+| `KK_AUTHENTIK_POST_ENROLLMENT_REDIRECT` | Redirect after new user enrollment |
+
+### Setting Up Authentik
+
+1. **Create an Application** in Authentik admin
+ - Name: `KrakenKey`
+ - Slug: `krakenkey`
+
+2. **Create an OAuth2/OIDC Provider**
+ - Client type: Confidential
+ - Client ID: Copy to `KK_AUTHENTIK_CLIENT_ID`
+ - Client Secret: Copy to `KK_AUTHENTIK_CLIENT_SECRET`
+ - Redirect URIs: Add your `KK_AUTHENTIK_REDIRECT_URI`
+ - Signing Key: Select an RSA key (RS256)
+ - Scopes: `openid`, `profile`, `email`
+
+3. **Create an Enrollment Flow** (for self-registration)
+ - Slug: Copy to `KK_AUTHENTIK_ENROLLMENT_SLUG`
+ - Configure stages: identification, password, email verification (optional)
+
+4. **Assign the provider** to the application
+
+### JWT Validation
+
+The backend validates JWTs using:
+- **JWKS endpoint**: Fetched from Authentik's `.well-known/openid-configuration`
+- **Algorithm**: RS256
+- **Issuer**: Must match `KK_AUTHENTIK_ISSUER_URL`
+
+### User Provisioning
+
+Users are created just-in-time on first OIDC callback:
+- User ID: Authentik `sub` claim
+- Username: `preferred_username` claim
+- Email: `email` claim
+- Groups: `groups` claim (used for admin detection)
+
+---
+
+## Stripe (Billing)
+
+### Overview
+
+KrakenKey uses Stripe for subscription billing. The integration supports checkout sessions, customer portal, subscription management, and webhook processing.
+
+### Configuration
+
+| Variable | Description |
+|----------|-------------|
+| `KK_STRIPE_SECRET_KEY` | Stripe secret API key |
+| `KK_STRIPE_WEBHOOK_SECRET` | Webhook signing secret |
+| `KK_STRIPE_PRICE_STARTER` | Price ID for Starter plan |
+| `KK_STRIPE_PRICE_TEAM` | Price ID for Team plan |
+| `KK_STRIPE_PRICE_BUSINESS` | Price ID for Business plan |
+| `KK_STRIPE_PRICE_ENTERPRISE` | Price ID for Enterprise plan |
+
+### Setting Up Stripe
+
+1. **Create Products and Prices** in the Stripe Dashboard
+ - Create a product for each plan (Starter, Team, Business, Enterprise)
+ - Create a recurring price for each product
+ - Copy each Price ID (e.g. `price_1ABC...`) to the corresponding env var
+
+2. **Set Up Webhooks**
+ - Go to Stripe Dashboard → Developers → Webhooks
+ - Add endpoint: `https://api.example.com/billing/webhook`
+ - Select events:
+ - `checkout.session.completed`
+ - `customer.subscription.updated`
+ - `customer.subscription.deleted`
+ - `invoice.payment_failed`
+ - Copy the signing secret to `KK_STRIPE_WEBHOOK_SECRET`
+
+3. **For local development**, use the Stripe CLI:
+
+```bash
+stripe listen --forward-to localhost:8080/billing/webhook
+```
+
+### Webhook Events
+
+| Event | Action |
+|-------|--------|
+| `checkout.session.completed` | Creates subscription record, links to user/org |
+| `customer.subscription.updated` | Updates plan, status, billing period |
+| `customer.subscription.deleted` | Marks subscription as canceled, resets to free tier |
+| `invoice.payment_failed` | Updates subscription status to `past_due` |
+
+---
+
+## Probe Service (Internal)
+
+### Overview
+
+KrakenKey supports external probe instances that connect to the API for TLS endpoint monitoring. Probes authenticate using service API keys.
+
+### Configuration
+
+| Variable | Description |
+|----------|-------------|
+| `KK_PROBE_API_KEY` | Pre-shared service key for probe authentication. Auto-seeded into the database on startup |
+
+### API Key Types
+
+| Prefix | Type | Purpose |
+|--------|------|---------|
+| `kk_` | User API key | Individual user access to the API |
+| `kk_svc_` | Service API key | System-level access for probes and internal services |
+
+### Authentication
+
+Service keys authenticate via the `Authorization: Bearer kk_svc_...` header and are validated using scrypt hash comparison. They support expiration and revocation.
diff --git a/backend/docs/ORGANIZATIONS.md b/backend/docs/ORGANIZATIONS.md
new file mode 100644
index 0000000..b153b06
--- /dev/null
+++ b/backend/docs/ORGANIZATIONS.md
@@ -0,0 +1,175 @@
+# Organizations
+
+Organizations allow teams to share domains, certificates, and endpoints under a single account with role-based access control.
+
+## Requirements
+
+- Organizations require a **Team** plan or higher
+- A user can only belong to one organization at a time
+- Users with an active paid personal subscription cannot join an organization (cancel first or let it lapse)
+
+## Roles
+
+| Role | Manage Members | Manage Settings | Billing | View Resources | Create Resources |
+|------|---------------|----------------|---------|---------------|-----------------|
+| **Owner** | Yes | Yes | Yes | Yes | Yes |
+| **Admin** | Yes | Yes | No | Yes | Yes |
+| **Member** | No | No | No | Yes | Yes |
+| **Viewer** | No | No | No | Yes | No |
+
+- Each organization has exactly one owner
+- Only owners can transfer ownership, delete the organization, or manage billing
+- Admins can invite/remove members and update settings but cannot change the owner or access billing
+- Members can create and manage resources (domains, certs, endpoints) but cannot manage the organization itself
+- Viewers have read-only access
+
+## API Endpoints
+
+### Create Organization
+
+```
+POST /organizations
+```
+
+**Request:**
+```json
+{
+ "name": "My Team"
+}
+```
+
+The creating user becomes the owner. Their personal subscription is converted to an organization subscription.
+
+Name must be 2–80 characters.
+
+### Get Organization
+
+```
+GET /organizations/:id
+```
+
+Returns the organization with its member list:
+
+```json
+{
+ "id": "uuid",
+ "name": "My Team",
+ "ownerId": "user-id",
+ "status": "active",
+ "createdAt": "2026-03-27T10:00:00.000Z",
+ "members": [
+ {
+ "id": "user-id",
+ "username": "alice",
+ "email": "alice@example.com",
+ "displayName": "Alice",
+ "role": "owner"
+ }
+ ]
+}
+```
+
+### Invite Member
+
+```
+POST /organizations/:id/members
+```
+
+**Request:**
+```json
+{
+ "email": "bob@example.com",
+ "role": "member"
+}
+```
+
+- The invited user must already have an account (have logged in at least once)
+- The user must not belong to another organization
+- The user must not have an active paid subscription
+- Default role is `member` if not specified
+- Available roles for invitation: `admin`, `member`, `viewer`
+
+### Remove Member
+
+```
+DELETE /organizations/:id/members/:userId
+```
+
+- Owners and admins can remove any non-owner member
+- Members can remove themselves (leave the organization)
+- The owner cannot be removed — use ownership transfer instead
+
+### Update Organization
+
+```
+PATCH /organizations/:id
+```
+
+**Request:**
+```json
+{
+ "name": "New Team Name"
+}
+```
+
+Requires owner or admin role.
+
+### Delete Organization
+
+```
+DELETE /organizations/:id
+```
+
+Owner only. Queues an asynchronous dissolution process:
+1. Non-owner member resources (domains, certificates) are transferred to the owner
+2. Member associations are cleared
+3. The organization subscription reverts to a personal subscription
+4. The organization record is deleted
+
+### Transfer Ownership
+
+```
+POST /organizations/:id/transfer-ownership
+```
+
+**Request:**
+```json
+{
+ "email": "bob@example.com"
+}
+```
+
+Owner only. The specified user must be a current member. The current owner becomes an admin after transfer.
+
+### Update Member Role
+
+```
+PATCH /organizations/:id/members/:userId
+```
+
+**Request:**
+```json
+{
+ "role": "admin"
+}
+```
+
+Requires owner or admin role. Available roles: `admin`, `member`, `viewer`. The `owner` role can only be assigned via the transfer ownership endpoint.
+
+## Organization Entity Schema
+
+| Column | Type | Description |
+|--------|------|-------------|
+| `id` | UUID | Primary key |
+| `name` | varchar | Organization name (2–80 chars) |
+| `ownerId` | varchar | Owner user ID (indexed) |
+| `status` | enum | `active` or `dissolving` |
+| `createdAt` | timestamp | Creation time |
+
+## Resource Sharing
+
+When a user belongs to an organization, all organization members can access each other's resources:
+- **Domains**: Shared across the team — any member's verified domain can authorize certificates for other members
+- **Certificates**: Visible to all members; write access depends on role
+- **Endpoints**: Shared monitoring across the team
+- **API Keys**: Individual per user (not shared)
diff --git a/backend/docs/README.md b/backend/docs/README.md
index 2b900b2..790469c 100644
--- a/backend/docs/README.md
+++ b/backend/docs/README.md
@@ -1,12 +1,27 @@
# KrakenKey Backend Documentation
-This directory contains comprehensive documentation for the KrakenKey backend API, a NestJS-based service for TLS certificate management and issuance via ACME protocol.
+This directory contains comprehensive documentation for the KrakenKey backend API, a NestJS-based service for TLS certificate management, endpoint monitoring, and team collaboration.
## Table of Contents
-1. [Architecture Overview](./ARCHITECTURE.md) - System design and module structure
-2. [API Reference](./API_REFERENCE.md) - Endpoint documentation
-3. [Database Schema](./DATABASE.md) - Data model and entities
+### Core
+1. [Architecture Overview](./ARCHITECTURE.md) — System design, module structure, data flows
+2. [API Reference](./API_REFERENCE.md) — Complete endpoint documentation
+3. [Database Schema](./DATABASE.md) — Entities, relationships, migrations
+
+### Setup
+4. [Configuration](./CONFIGURATION.md) — Environment variables reference
+5. [Integrations](./INTEGRATIONS.md) — Cloudflare, Let's Encrypt, Authentik, Stripe setup
+
+### Features
+6. [Certificate Flow](./CERTIFICATE_FLOW.md) — CSR generation, ACME issuance, renewal, revocation
+7. [Billing](./BILLING.md) — Plans, subscriptions, Stripe integration
+8. [Organizations](./ORGANIZATIONS.md) — Teams, roles, resource sharing
+9. [Endpoints](./ENDPOINTS.md) — TLS endpoint monitoring and probes
+
+### Guides
+10. [Domain Verification](../docs/DOMAIN_VERIFICATION_GUIDE.md) — DNS TXT verification walkthrough
+11. [Error Handling](../docs/ERROR_HANDLING.md) — Frontend and backend error handling patterns
## Quick Start
@@ -14,8 +29,9 @@ This directory contains comprehensive documentation for the KrakenKey backend AP
- Node.js 18+
- PostgreSQL 12+
- Redis 6+ (for BullMQ queue)
-- Cloudflare account (for DNS challenge)
-- Let's Encrypt ACME account
+- Cloudflare or AWS Route 53 account (for DNS-01 challenges)
+- Let's Encrypt ACME account key
+- Authentik instance (for OIDC authentication)
### Installation
@@ -25,96 +41,55 @@ yarn install
# Set up environment variables
cp .env.example .env
-
-# Create database
-yarn run db:create
+# Edit .env — see docs/CONFIGURATION.md for all variables
# Start in development mode
-yarn run start:dev
+yarn start:dev
```
+The API runs on port 8080 by default. Swagger docs are available at `/swagger`.
+
### Running Tests
```bash
-# Unit tests
-yarn run test
-
-# Test coverage
-yarn run test:cov
-
-# E2E tests
-yarn run test:e2e
+yarn test # unit tests
+yarn test:cov # coverage
+yarn test:e2e # end-to-end
```
## Project Structure
```
src/
-├── app.module.ts # Root module
-├── app.controller.ts # Root controller (health check)
-├── app.service.ts # Root service
├── main.ts # Application entry point
-└── certs/ # Certificate management module
- ├── certs.module.ts
- ├── certs.controller.ts
- ├── certs.service.ts
- └── tls/ # TLS certificate issuance submodule
- ├── tls.module.ts
- ├── tls.controller.ts
- ├── tls.service.ts
- ├── entities/ # Database entities
- ├── dto/ # Data transfer objects
- ├── interfaces/ # TypeScript interfaces
- ├── services/ # ACME and DNS strategies
- ├── processors/ # BullMQ job processors
- └── util/ # Utilities (CSR parsing)
+├── app.module.ts # Root module
+├── app.controller.ts # Health check endpoint
+├── auth/ # OIDC login, JWT, API key management
+├── users/ # User entities and account management
+├── domains/ # Domain registration and DNS verification
+├── certs/ # Certificate management
+│ └── tls/ # TLS certificate issuance (ACME, DNS-01)
+│ ├── entities/ # TlsCrt entity
+│ ├── dto/ # Request/response DTOs
+│ ├── services/ # ACME issuer, DNS strategies, CSR utils
+│ └── processors/ # BullMQ job processors
+├── billing/ # Stripe subscriptions and plan limits
+│ ├── entities/ # Subscription entity
+│ ├── constants/ # Plan limit definitions
+│ └── processors/ # Org dissolution processor
+├── organizations/ # Team management and RBAC
+├── endpoints/ # TLS endpoint monitoring
+│ └── entities/ # Endpoint, hosted region, probe assignment
+├── health/ # Health checks
+├── metrics/ # Prometheus metrics
+├── notifications/ # Email notifications
+├── feedback/ # User feedback
+├── probes/ # Kubernetes readiness/liveness probes
+├── throttler/ # Rate limiting
+├── filters/ # Global exception filter
+└── migrations/ # Database migrations
```
-## Key Features
-
-- **TLS Certificate Request Handling**: Submit Certificate Signing Requests (CSRs) for processing
-- **ACME Protocol Integration**: Automated Certificate Management Environment support for Let's Encrypt
-- **DNS-01 Challenge**: Cloudflare DNS provider for DNS-01 ACME challenges
-- **Job Queue**: BullMQ-based asynchronous certificate issuance pipeline
-- **CSR Validation**: Comprehensive CSR signature, domain, and key strength validation
-- **API Documentation**: Swagger/OpenAPI documentation at `/swagger`
-
-## Main Modules
-
-### App Module
-- **Purpose**: Root application module
-- **Endpoints**: Health check endpoint `/` returning API status and version
-- **Dependencies**: TypeORM, ConfigModule, BullModule
-
-### Certs Module
-- **Purpose**: Container for certificate management functionality
-- **Submodules**: TLS module
-
-### TLS Module
-- **Purpose**: Core functionality for TLS certificate issuance
-- **Endpoints**:
- - `POST /certs/tls` - Submit CSR for certificate issuance
- - `GET /certs/tls/:id` - Retrieve certificate status and data
- - `PATCH /certs/tls/:id` - Update certificate information
- - `DELETE /certs/tls/:id` - Revoke certificate
-- **Key Services**:
- - TlsService: Business logic for certificate management
- - AcmeIssuerStrategy: ACME protocol implementation
- - CloudflareDnsStrategy: DNS challenge management
- - CsrUtilService: CSR validation and parsing
-
-## External Integrations
-
-### Let's Encrypt (ACME)
-- **Purpose**: Certificate authority for issuing TLS certificates
-- **Environment**: Staging environment by default (configurable)
-- **Key Library**: `acme-client`
-
-### Cloudflare DNS
-- **Purpose**: DNS provider for DNS-01 challenge solving
-- **Key Features**: Automated record creation/deletion, TXT record management
-- **Key Library**: `cloudflare` SDK
-
## Technology Stack
- **Framework**: NestJS 11.x
@@ -122,26 +97,8 @@ src/
- **Database**: PostgreSQL with TypeORM
- **Queue**: BullMQ (Redis-backed)
- **ACME Client**: acme-client
-- **DNS Management**: Cloudflare SDK
+- **DNS Providers**: Cloudflare SDK, AWS SDK (Route 53)
+- **Authentication**: Authentik (OIDC), JWT, API keys
+- **Billing**: Stripe
- **Cryptography**: node-forge
- **Documentation**: Swagger/OpenAPI
-
-## Security Considerations
-
-- CSR signatures are verified using embedded public keys
-- Minimum RSA key length: 2048 bits
-- Domain authorization via DNS-01 challenges
-- Secure credential storage via environment variables
-- Certificate validation with Let's Encrypt staging/production endpoints
-
-## Performance & Scalability
-
-- Asynchronous job processing with BullMQ
-- Exponential backoff retry strategy for failed operations
-- DNS propagation polling with configurable timeout
-- Database connection pooling via TypeORM
-- PostgreSQL for persistent storage
-
-## For More Details
-
-Refer to the specific documentation files listed in the Table of Contents for detailed information about architecture, API endpoints, database schema, and integration procedures.