Debian Codegraph @

captcha.go

raw symbols SCIP
1 // Copyright 2011 Dmitry Chestnykh. All rights reserved.
2 // Use of this source code is governed by a MIT-style
3 // license that can be found in the LICENSE file.
4
5 // Package captcha implements generation and verification of image and audio
6 // CAPTCHAs.
7 //
8 // A captcha solution is the sequence of digits 0-9 with the defined length.
9 // There are two captcha representations: image and audio.
10 //
11 // An image representation is a PNG-encoded image with the solution printed on
12 // it in such a way that makes it hard for computers to solve it using OCR.
13 //
14 // An audio representation is a WAVE-encoded (8 kHz unsigned 8-bit) sound with
15 // the spoken solution (currently in English, Russian, Chinese, and Japanese).
16 // To make it hard for computers to solve audio captcha, the voice that
17 // pronounces numbers has random speed and pitch, and there is a randomly
18 // generated background noise mixed into the sound.
19 //
20 // This package doesn't require external files or libraries to generate captcha
21 // representations; it is self-contained.
22 //
23 // To make captchas one-time, the package includes a memory storage that stores
24 // captcha ids, their solutions, and expiration time. Used captchas are removed
25 // from the store immediately after calling Verify or VerifyString, while
26 // unused captchas (user loaded a page with captcha, but didn't submit the
27 // form) are collected automatically after the predefined expiration time.
28 // Developers can also provide custom store (for example, which saves captcha
29 // ids and solutions in database) by implementing Store interface and
30 // registering the object with SetCustomStore.
31 //
32 // Captchas are created by calling New, which returns the captcha id. Their
33 // representations, though, are created on-the-fly by calling WriteImage or
34 // WriteAudio functions. Created representations are not stored anywhere, but
35 // subsequent calls to these functions with the same id will write the same
36 // captcha solution. Reload function will create a new different solution for
37 // the provided captcha, allowing users to "reload" captcha if they can't solve
38 // the displayed one without reloading the whole page. Verify and VerifyString
39 // are used to verify that the given solution is the right one for the given
40 // captcha id.
41 //
42 // Server provides an http.Handler which can serve image and audio
43 // representations of captchas automatically from the URL. It can also be used
44 // to reload captchas. Refer to Server function documentation for details, or
45 // take a look at the example in "capexample" subdirectory.
46 package captchascip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/
47
48 import (
49 "bytesscip-go gomod github.com/golang/go/src go1.25 bytes/"
50 "errorsscip-go gomod github.com/golang/go/src go1.25 errors/"
51 "ioscip-go gomod github.com/golang/go/src go1.25 io/"
52 "timescip-go gomod github.com/golang/go/src go1.25 time/"
53 )
54
55 const (
56 // Default number of digits in captcha solution.
57 DefaultLenscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/DefaultLen. = 6
58 // The number of captchas created that triggers garbage collection used
59 // by default store.
60 CollectNumscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/CollectNum. = 100
61 // Expiration time of captchas used by default store.
62 Expirationscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/Expiration. = 10 * timescip-go gomod github.com/golang/go/src go1.25 time/.Minutescip-go gomod github.com/golang/go/src go1.25 time/Minute.
63 )
64
65 var (
66 ErrNotFoundscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/ErrNotFound. = errorsscip-go gomod github.com/golang/go/src go1.25 errors/.Newscip-go gomod github.com/golang/go/src go1.25 errors/New().("captcha: id not found")
67 // globalStore is a shared storage for captchas, generated by New function.
68 globalStorescip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/globalStore. = NewMemoryStorescip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/NewMemoryStore().(CollectNumscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/CollectNum., Expirationscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/Expiration.)
69 )
70
71 // SetCustomStore sets custom storage for captchas, replacing the default
72 // memory store. This function must be called before generating any captchas.
73 func SetCustomStorescip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/SetCustomStore().(s Storescip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/Store#) {
74 globalStorescip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/globalStore. = s
75 }
76
77 // New creates a new captcha with the standard length, saves it in the internal
78 // storage and returns its id.
79 func Newscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/New().() string {
80 return NewLenscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/NewLen().(DefaultLenscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/DefaultLen.)
81 }
82
83 // NewLen is just like New, but accepts length of a captcha solution as the
84 // argument.
85 func NewLenscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/NewLen().(length int) (id string) {
86 id = randomIDscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/randomID().()
87 globalStorescip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/globalStore..Setscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/Store#Set.(id, RandomDigitsscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/RandomDigits().(length))
88 return id
89 }
90
91 // Reload generates and remembers new digits for the given captcha id. This
92 // function returns false if there is no captcha with the given id.
93 //
94 // After calling this function, the image or audio presented to a user must be
95 // refreshed to show the new captcha representation (WriteImage and WriteAudio
96 // will write the new one).
97 func Reloadscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/Reload().(id string) bool {
98 old := globalStorescip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/globalStore..Getscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/Store#Get.(id, false)
99 if old == nil {
100 return false
101 }
102 globalStorescip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/globalStore..Setscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/Store#Set.(id, RandomDigitsscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/RandomDigits().(len(old)))
103 return true
104 }
105
106 // WriteImage writes PNG-encoded image representation of the captcha with the
107 // given id. The image will have the given width and height.
108 func WriteImagescip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/WriteImage().(w ioscip-go gomod github.com/golang/go/src go1.25 io/.Writerscip-go gomod github.com/golang/go/src go1.25 io/Writer#, id string, width, height int) error {
109 d := globalStorescip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/globalStore..Getscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/Store#Get.(id, false)
110 if d == nil {
111 return ErrNotFoundscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/ErrNotFound.
112 }
113 _, err := NewImagescip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/NewImage().(id, d, width, height).WriteToscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/Image#WriteTo().(w)
114 return err
115 }
116
117 // Verify returns true if the given digits are the ones that were used to
118 // create the given captcha id.
119 //
120 // The function deletes the captcha with the given id from the internal
121 // storage, so that the same captcha can't be verified anymore.
122 func Verifyscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/Verify().(id string, digits []byte) bool {
123 if len(digits) == 0 {
124 return false
125 }
126 reald := globalStorescip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/globalStore..Getscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/Store#Get.(id, true)
127 if reald == nil {
128 return false
129 }
130 return bytesscip-go gomod github.com/golang/go/src go1.25 bytes/.Equalscip-go gomod github.com/golang/go/src go1.25 bytes/Equal().(digits, reald)
131 }
132
133 // VerifyString is like Verify, but accepts a string of digits. It removes
134 // spaces and commas from the string, but any other characters, apart from
135 // digits and listed above, will cause the function to return false.
136 func VerifyStringscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/VerifyString().(id string, digits string) bool {
137 if digits == "" {
138 return false
139 }
140 ns := make([]byte, len(digits))
141 for i := range ns {
142 d := digits[i]
143 switch {
144 case '0' <= d && d <= '9':
145 ns[i] = d - '0'
146 case d == ' ' || d == ',':
147 // ignore
148 default:
149 return false
150 }
151 }
152 return Verifyscip-go gomod code.forgejo.org/go-chi/captcha . `code.forgejo.org/go-chi/captcha`/Verify().(id, ns)
153 }