]> gitweb.factorcode.org Git - factor.git/blob - basis/furnace/auth/auth-docs.factor
a05dd3c605cc2e60c7af3acc07f6c4cdb81c694a
[factor.git] / basis / furnace / auth / auth-docs.factor
1 USING: byte-arrays checksums.sha furnace.auth.providers
2 furnace.auth.providers.db help.markup help.syntax http kernel
3 math strings vocabs words.symbol ;
4 "furnace.auth.basic" require
5 "furnace.auth.features.deactivate-user" require
6 "furnace.auth.features.edit-profile" require
7 "furnace.auth.features.recover-password" require
8 "furnace.auth.features.registration" require
9 "furnace.auth.login" require
10 "furnace.auth.providers.assoc" require
11 "furnace.auth.providers.null" require
12 IN: furnace.auth
13
14 HELP: <protected>
15 { $values
16      { "responder" "a responder" }
17      { "protected" "a new responder" }
18 }
19 { $description "Wraps a responder in a protected responder. Access to the wrapped responder will be conditional upon the client authenticating with the current authentication realm." } ;
20
21 HELP: >>encoded-password
22 { $values { "user" user } { "string" string } }
23 { $description "Sets the user's password by combining it with a random salt and encoding it with the current authentication realm's checksum." } ;
24
25 HELP: capabilities
26 { $var-description "Global variable holding all defined capabilities. New capabilities may be defined with " { $link define-capability } "." } ;
27
28 HELP: check-login
29 { $values { "password" string } { "username" string } { "user/f" { $maybe user } } }
30 { $description "Checks a username/password pair with the current authentication realm. Outputs a user if authentication succeeded, otherwise outputs " { $link f } "." } ;
31
32 HELP: define-capability
33 { $values { "word" symbol } }
34 { $description "Defines a new capability by adding it to the " { $link capabilities } " global variable." } ;
35
36 HELP: encode-password
37 { $values
38      { "string" string } { "salt" integer }
39      { "bytes" byte-array }
40 }
41 { $description "Encodes a password with the current authentication realm's checksum." } ;
42
43 HELP: have-capabilities?
44 { $values
45      { "capabilities" "a sequence of capabilities" }
46      { "?" boolean }
47 }
48 { $description "Tests if the currently logged-in user possesses the given capabilities." } ;
49
50 HELP: logged-in-user
51 { $var-description "Holds the currently logged-in user." } ;
52
53 HELP: login-required
54 { $values
55      { "description" string } { "capabilities" "a sequence of capabilities" }
56 }
57 { $description "Redirects the client to a login page." } ;
58
59 HELP: login-required*
60 { $values
61      { "description" string } { "capabilities" "a sequence of capabilities" } { "realm" "an authenticaiton realm" }
62      { "response" response }
63 }
64 { $contract "Constructs an HTTP response for redirecting the client to a login page." } ;
65
66 HELP: protected
67 { $class-description "The class of protected responders. See " { $link "furnace.auth.protected" } " for a description of usage and slots." } ;
68
69 HELP: realm
70 { $class-description "The class of authentication realms. See " { $link "furnace.auth.realms" } " for details." } ;
71
72 HELP: uchange
73 { $values { "quot" { $quotation ( old -- new ) } } { "key" symbol } }
74 { $description "Applies the quotation to the old value of the user profile variable, and assigns the resulting value back to the variable." } ;
75
76 HELP: uget
77 { $values { "key" symbol } { "value" object } }
78 { $description "Outputs the value of a user profile variable." } ;
79
80 HELP: uset
81 { $values { "value" object } { "key" symbol } }
82 { $description "Sets the value of a user profile variable." } ;
83
84 HELP: username
85 { $values { "string/f" { $maybe string } }
86 }
87 { $description "Outputs the currently logged-in username, or " { $link f } " if no user is logged in." } ;
88 HELP: users
89 { $values { "provider" "an authentication provider" } }
90 { $description "Outputs the current authentication provider." } ;
91
92 ARTICLE: "furnace.auth.capabilities" "Authentication capabilities"
93 "Every user in the authentication framework has a set of associated capabilities."
94 $nl
95 "Defining new capabilities:"
96 { $subsections define-capability }
97 "Capabilities are stored in a global variable:"
98 { $subsections capabilities }
99 "Protected resources can be restricted to users possessing certain capabilities only by storing a sequence of capabilities in the " { $slot "capabilities" } " slot of a " { $link protected } " instance." ;
100
101 ARTICLE: "furnace.auth.protected" "Protected resources"
102 "To restrict access to authenticated clients only, wrap a responder in a protected responder."
103 { $subsections
104     protected
105     <protected>
106 }
107 "Protected responders have the following two slots which may be set:"
108 { $table
109     { { $slot "description" } "A string identifying the protected resource for user interface purposes" }
110     { { $slot "capabilities" } { "A sequence of capabilities; see " { $link "furnace.auth.capabilities" } } }
111 } ;
112
113 ARTICLE: "furnace.auth.realm-config" "Authentication realm configuration"
114 "Instances of subclasses of " { $link realm } " have the following slots which may be set:"
115 { $table
116     { { $slot "name" } "A string identifying the realm for user interface purposes" }
117     { { $slot "users" } { "An authentication provider (see " { $link "furnace.auth.providers" } "). By default, the " { $link users-in-db } " provider is used." } }
118     { { $slot "checksum" } { "An implementation of the checksum protocol used for verifying passwords (see " { $link "checksums" } "). The " { $link sha-256 } " checksum is used by default." } }
119     { { $slot "secure" } { "A boolean, that when set to a true value, forces the client to access the authentication realm via HTTPS. An attempt to access the realm via HTTP results in a redirect to the corresponding HTTPS URL. On by default." } }
120 } ;
121
122 ARTICLE: "furnace.auth.providers" "Authentication providers"
123 "The " { $vocab-link "furnace.auth" } " framework looks up users using an authentication provider. Different authentication providers can be swapped in to implement various authentication strategies."
124 $nl
125 "Each authentication realm has a provider stored in the " { $slot "users" } " slot. The default provider is " { $link users-in-db } "."
126 { $subsections
127     "furnace.auth.providers.protocol"
128     "furnace.auth.providers.null"
129     "furnace.auth.providers.assoc"
130     "furnace.auth.providers.db"
131 } ;
132
133 ARTICLE: "furnace.auth.features" "Optional authentication features"
134 "Vocabularies having names prefixed by " { $code "furnace.auth.features" } " implement optional features which can be enabled by calling special words. These words define new actions on an authentication realm."
135 { $subsections
136     "furnace.auth.features.deactivate-user"
137     "furnace.auth.features.edit-profile"
138     "furnace.auth.features.recover-password"
139     "furnace.auth.features.registration"
140 } ;
141
142 ARTICLE: "furnace.auth.realms" "Authentication realms"
143 "The superclass of authentication realms:"
144 { $subsections realm }
145 "There are two concrete implementations:"
146 { $subsections
147     "furnace.auth.basic"
148     "furnace.auth.login"
149 }
150 "Authentication realms need to be configured after construction."
151 { $subsections "furnace.auth.realm-config" } ;
152
153 ARTICLE: "furnace.auth.users" "User profiles"
154 "A responder wrapped in an authentication realm may access the currently logged-in user,"
155 { $subsections logged-in-user }
156 "as well as the logged-in username:"
157 { $subsections username }
158 "Values can also be stored in user profile variables:"
159 { $subsections
160     uget
161     uset
162     uchange
163 }
164 "User profile variables have the same restrictions on their values as session variables; see " { $link "furnace.sessions.serialize" } " for a discussion." ;
165
166 ARTICLE: "furnace.auth.example" "Furnace authentication example"
167 "The " { $vocab-link "webapps.todo" } " vocabulary wraps all of its responders in a protected responder. The " { $slot "description" } " slot is set so that the login page contains the message “You must log in to view your todo list”:"
168 { $code
169     "<protected>
170     \"view your todo list\" >>description"
171 }
172 "The " { $vocab-link "webapps.wiki" } " vocabulary defines a mix of protected and unprotected actions. One example of a protected action is that for deleting wiki pages, an action normally reserved for administrators. This action is protected with the following code:"
173 { $code
174     "<protected>
175     \"delete wiki articles\" >>description
176     { can-delete-wiki-articles? } >>capabilities"
177 }
178 "The " { $vocab-link "websites.concatenative" } " vocabulary wraps all of its responders, including the wiki, in a login authentication realm:"
179 { $code
180 ": <login-config> ( responder -- responder' )
181     \"Factor website\" <login-realm>
182         allow-registration
183         allow-password-recovery
184         allow-edit-profile
185         allow-deactivation ;"
186 } ;
187
188 ARTICLE: "furnace.auth" "Furnace authentication"
189 "The " { $vocab-link "furnace.auth" } " vocabulary implements a pluggable authentication framework."
190 $nl
191 "Usernames and passwords are verified using an " { $emphasis "authentication provider" } "."
192 { $subsections "furnace.auth.providers" }
193 "Users have capabilities assigned to them."
194 { $subsections "furnace.auth.capabilities" }
195 "An " { $emphasis "authentication realm" } " is a responder which manages access to protected resources."
196 { $subsections "furnace.auth.realms" }
197 "Actions contained inside an authentication realm can be protected by wrapping them with a responder."
198 { $subsections "furnace.auth.protected" }
199 "Actions contained inside an authentication realm can access the currently logged-in user profile."
200 { $subsections "furnace.auth.users" }
201 "Authentication realms can be adorned with additional functionality."
202 { $subsections "furnace.auth.features" }
203 "A concrete example."
204 { $subsections "furnace.auth.example" } ;
205
206 ABOUT: "furnace.auth"