git.iko.soy/typeable/oauth2-proxy
on v2.2 help sign in
74 lines 2.0 kB history raw
1package providers
3import (
4 "io/ioutil"
5 "log"
6 "net/http"
7 "net/url"
9 "github.com/bitly/oauth2_proxy/api"
12// stripToken is a helper function to obfuscate "access_token"
13// query parameters
14func stripToken(endpoint string) string {
15 return stripParam("access_token", endpoint)
18// stripParam generalizes the obfuscation of a particular
19// query parameter - typically 'access_token' or 'client_secret'
20// The parameter's second half is replaced by '...' and returned
21// as part of the encoded query parameters.
22// If the target parameter isn't found, the endpoint is returned
23// unmodified.
24func stripParam(param, endpoint string) string {
25 u, err := url.Parse(endpoint)
26 if err != nil {
27 log.Printf("error attempting to strip %s: %s", param, err)
28 return endpoint
29 }
31 if u.RawQuery != "" {
32 values, err := url.ParseQuery(u.RawQuery)
33 if err != nil {
34 log.Printf("error attempting to strip %s: %s", param, err)
35 return u.String()
36 }
38 if val := values.Get(param); val != "" {
39 values.Set(param, val[:(len(val)/2)]+"...")
40 u.RawQuery = values.Encode()
41 return u.String()
42 }
43 }
45 return endpoint
48// validateToken returns true if token is valid
49func validateToken(p Provider, access_token string, header http.Header) bool {
50 if access_token == "" || p.Data().ValidateURL == nil {
51 return false
52 }
53 endpoint := p.Data().ValidateURL.String()
54 if len(header) == 0 {
55 params := url.Values{"access_token": {access_token}}
56 endpoint = endpoint + "?" + params.Encode()
57 }
58 resp, err := api.RequestUnparsedResponse(endpoint, header)
59 if err != nil {
60 log.Printf("GET %s", endpoint)
61 log.Printf("token validation request failed: %s", err)
62 return false
63 }
65 body, _ := ioutil.ReadAll(resp.Body)
66 resp.Body.Close()
67 log.Printf("%d GET %s %s", resp.StatusCode, stripToken(endpoint), body)
69 if resp.StatusCode == 200 {
70 return true
71 }
72 log.Printf("token validation request failed: status %d - %s", resp.StatusCode, body)
73 return false