Debian Codegraph @

README.md

raw symbols SCIP
1 Package captcha
2 =====================
3
4 This is a fork of https://github.com/dchest/captcha
5
6 **:warning: Warning: this captcha can be broken by advanced OCR captcha breaking algorithms.**
7
8 import "code.forgejo.org/go-chi/captcha"
9
10 Package captcha implements generation and verification of image CAPTCHAs.
11
12 A captcha solution is the sequence of digits 0-9 with the defined length.
13 There is one captcha representations: image.
14
15 An image representation is a PNG-encoded image with the solution printed on
16 it in such a way that makes it hard for computers to solve it using OCR.
17
18 This package doesn't require external files or libraries to generate captcha
19 representations; it is self-contained.
20
21 To make captchas one-time, the package includes a memory storage that stores
22 captcha ids, their solutions, and expiration time. Used captchas are removed
23 from the store immediately after calling Verify or VerifyString, while
24 unused captchas (user loaded a page with captcha, but didn't submit the
25 form) are collected automatically after the predefined expiration time.
26 Developers can also provide custom store (for example, which saves captcha
27 ids and solutions in database) by implementing Store interface and
28 registering the object with SetCustomStore.
29
30 Captchas are created by calling New, which returns the captcha id. Their
31 representations, though, are created on-the-fly by calling WriteImage function.
32 Created representations are not stored anywhere, but subsequent calls to these
33 functions with the same id will write the same captcha solution. Reload function
34 will create a new different solution for the provided captcha, allowing users to
35 "reload" captcha if they can't solve the displayed one without reloading the
36 whole page. Verify and VerifyString are used to verify that the given solution
37 is the right one for the given captcha id.
38
39 Server provides an http.Handler which can serve image representations of
40 captchas automatically from the URL. It can also be used to reload captchas.
41 Refer to Server function documentation for details, or take a look at the
42 example in "capexample" subdirectory.
43
44
45 Examples
46 --------
47
48 ![Image](https://github.com/dchest/captcha/raw/master/capgen/example.png)
49
50 Constants
51 ---------
52
53 ``` go
54 const (
55 // Default number of digits in captcha solution.
56 DefaultLen = 6
57 // The number of captchas created that triggers garbage collection used
58 // by default store.
59 CollectNum = 100
60 // Expiration time of captchas used by default store.
61 Expiration = 10 * time.Minute
62 )
63 ```
64
65 ``` go
66 const (
67 // Standard width and height of a captcha image.
68 StdWidth = 240
69 StdHeight = 80
70 )
71 ```
72
73
74 Variables
75 ---------
76
77 ``` go
78 var (
79 ErrNotFound = errors.New("captcha: id not found")
80 )
81 ```
82
83
84
85 Functions
86 ---------
87
88 ### func New
89
90 func New() string
91
92 New creates a new captcha with the standard length, saves it in the internal
93 storage and returns its id.
94
95 ### func NewLen
96
97 func NewLen(length int) (id string)
98
99 NewLen is just like New, but accepts length of a captcha solution as the
100 argument.
101
102 ### func RandomDigits
103
104 func RandomDigits(length int) (b []byte)
105
106 RandomDigits returns a byte slice of the given length containing
107 pseudorandom numbers in range 0-9. The slice can be used as a captcha
108 solution.
109
110 ### func Reload
111
112 func Reload(id string) bool
113
114 Reload generates and remembers new digits for the given captcha id. This
115 function returns false if there is no captcha with the given id.
116
117 After calling this function, the image presented to a user must be refreshed to
118 show the new captcha representation (WriteImage will write the new one).
119
120 ### func Server
121
122 func Server(imgWidth, imgHeight int) http.Handler
123
124 Server returns a handler that serves HTTP requests with image or
125 representations of captchas. Image dimensions are accepted as
126 arguments. The server decides which captcha to serve based on the last URL
127 path component: file name part must contain a captcha id, file extension —
128 its format (PNG or WAV).
129
130 For example, for file name "LBm5vMjHDtdUfaWYXiQX.png" it serves an image captcha
131 with id "LBm5vMjHDtdUfaWYXiQX".
132
133 To reload captcha (get a different solution for the same captcha id), append
134 "?reload=x" to URL, where x may be anything (for example, current time or a
135 random number to make browsers refetch an image instead of loading it from
136 cache).
137
138 ### func SetCustomStore
139
140 func SetCustomStore(s Store)
141
142 SetCustomStore sets custom storage for captchas, replacing the default
143 memory store. This function must be called before generating any captchas.
144
145 ### func Verify
146
147 func Verify(id string, digits []byte) bool
148
149 Verify returns true if the given digits are the ones that were used to
150 create the given captcha id.
151
152 The function deletes the captcha with the given id from the internal
153 storage, so that the same captcha can't be verified anymore.
154
155 ### func VerifyString
156
157 func VerifyString(id string, digits string) bool
158
159 VerifyString is like Verify, but accepts a string of digits. It removes
160 spaces and commas from the string, but any other characters, apart from
161 digits and listed above, will cause the function to return false.
162
163 ### func WriteImage
164
165 func WriteImage(w io.Writer, id string, width, height int) error
166
167 WriteImage writes PNG-encoded image representation of the captcha with the
168 given id. The image will have the given width and height.
169
170
171 Types
172 -----
173
174 ``` go
175 type Image struct {
176 *image.Paletted
177 // contains unexported fields
178 }
179 ```
180
181
182 ### func NewImage
183
184 func NewImage(id string, digits []byte, width, height int) *Image
185
186 NewImage returns a new captcha image of the given width and height with the
187 given digits, where each digit must be in range 0-9.
188
189 ### func (*Image) WriteTo
190
191 func (m *Image) WriteTo(w io.Writer) (int64, error)
192
193 WriteTo writes captcha image in PNG format into the given writer.
194
195 ``` go
196 type Store interface {
197 // Set sets the digits for the captcha id.
198 Set(id string, digits []byte)
199
200 // Get returns stored digits for the captcha id. Clear indicates
201 // whether the captcha must be deleted from the store.
202 Get(id string, clear bool) (digits []byte)
203 }
204 ```
205
206 An object implementing Store interface can be registered with SetCustomStore
207 function to handle storage and retrieval of captcha ids and solutions for
208 them, replacing the default memory store.
209
210 It is the responsibility of an object to delete expired and used captchas
211 when necessary (for example, the default memory store collects them in Set
212 method after the certain amount of captchas has been stored.)
213
214 ### func NewMemoryStore
215
216 func NewMemoryStore(collectNum int, expiration time.Duration) Store
217
218 NewMemoryStore returns a new standard memory store for captchas with the
219 given collection threshold and expiration time in seconds. The returned
220 store must be registered with SetCustomStore to replace the default one.