|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/sha1" |
| 5 | + "encoding/hex" |
| 6 | + "encoding/json" |
| 7 | + "io/ioutil" |
| 8 | + "log" |
| 9 | + "net/http" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/aws/aws-lambda-go/events" |
| 13 | + "github.com/aws/aws-lambda-go/lambda" |
| 14 | +) |
| 15 | + |
| 16 | +type pwdRequest struct { |
| 17 | + Password string `json:"password"` |
| 18 | +} |
| 19 | + |
| 20 | +type pwdResponse struct { |
| 21 | + Password string `json:"password"` |
| 22 | + Occurrences string `json:"occurrences"` |
| 23 | + Sha1 string `json:"sha1"` |
| 24 | +} |
| 25 | + |
| 26 | +func router(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { |
| 27 | + log.Printf("req-method: %s", req.HTTPMethod) |
| 28 | + if req.Path == "/check-password" { |
| 29 | + if req.HTTPMethod == "POST" { |
| 30 | + return checkPasswordHandler(req) |
| 31 | + } |
| 32 | + } |
| 33 | + return events.APIGatewayProxyResponse{ |
| 34 | + StatusCode: http.StatusMethodNotAllowed, |
| 35 | + Body: http.StatusText(http.StatusMethodNotAllowed), |
| 36 | + }, nil |
| 37 | +} |
| 38 | + |
| 39 | +func checkPasswordHandler(req events.APIGatewayProxyRequest) (events.APIGatewayProxyResponse, error) { |
| 40 | + var request pwdRequest |
| 41 | + err := json.Unmarshal([]byte(req.Body), &request) |
| 42 | + if err != nil { |
| 43 | + return events.APIGatewayProxyResponse{ |
| 44 | + StatusCode: http.StatusInternalServerError, |
| 45 | + Body: http.StatusText(http.StatusInternalServerError), |
| 46 | + }, nil |
| 47 | + } |
| 48 | + response, err := json.Marshal(checkPasswordService(request)) |
| 49 | + if err != nil { |
| 50 | + return events.APIGatewayProxyResponse{ |
| 51 | + StatusCode: http.StatusInternalServerError, |
| 52 | + Body: http.StatusText(http.StatusInternalServerError), |
| 53 | + }, nil |
| 54 | + } |
| 55 | + return events.APIGatewayProxyResponse{ |
| 56 | + StatusCode: http.StatusOK, |
| 57 | + Body: string(response), |
| 58 | + }, nil |
| 59 | +} |
| 60 | + |
| 61 | +func checkPasswordService(request pwdRequest) pwdResponse { |
| 62 | + var api string = "https://api.pwnedpasswords.com/range/" |
| 63 | + pwd := request.Password |
| 64 | + hash := sha1.New() |
| 65 | + hash.Write([]byte(pwd)) |
| 66 | + result := strings.ToUpper(hex.EncodeToString(hash.Sum(nil))) |
| 67 | + response, err := http.Get(api + result[0:5]) |
| 68 | + if err != nil { |
| 69 | + log.Fatalln(err) |
| 70 | + } |
| 71 | + defer response.Body.Close() |
| 72 | + orig := result |
| 73 | + result = result[5:len(result)] |
| 74 | + contents, err := ioutil.ReadAll(response.Body) |
| 75 | + if err != nil { |
| 76 | + log.Fatalln(err) |
| 77 | + } |
| 78 | + passwords := strings.Split(string(contents), "\n") |
| 79 | + pwdMap := make(map[string]string) |
| 80 | + var currentSplit []string |
| 81 | + for i := 0; i < len(passwords); i++ { |
| 82 | + currentSplit = strings.Split(passwords[i], ":") |
| 83 | + pwdMap[currentSplit[0]] = currentSplit[1][0 : len(currentSplit[1])-1] |
| 84 | + } |
| 85 | + checkPwd := pwdMap[result] |
| 86 | + var httpResponse pwdResponse |
| 87 | + if pwdMap[result] != "" { |
| 88 | + httpResponse.Password = pwd |
| 89 | + httpResponse.Occurrences = checkPwd |
| 90 | + httpResponse.Sha1 = orig |
| 91 | + } |
| 92 | + return httpResponse |
| 93 | +} |
| 94 | +func main() { |
| 95 | + lambda.Start(router) |
| 96 | +} |
0 commit comments