MCPcopy Create free account
hub / github.com/MG-RAST/Shock / authToken

Function authToken

shock-server/auth/oauth/oauth.go:56–98  ·  view source on GitHub ↗

authToken validiates token by fetching user information.

(token string, url string)

Source from the content-addressed store, hash-verified

54
55// authToken validiates token by fetching user information.
56func authToken(token string, url string) (u *user.User, err error) {
57 client := &http.Client{
58 Transport: &http.Transport{TLSClientConfig: &tls.Config{InsecureSkipVerify: true}},
59 }
60 req, err := http.NewRequest("GET", url, nil)
61 if err != nil {
62 return nil, errors.New("(oauth) HTTP GET: " + err.Error())
63 }
64 req.Header.Add("Auth", token)
65 if resp, err := client.Do(req); err == nil {
66 defer resp.Body.Close()
67 if resp.StatusCode == http.StatusOK {
68 if body, err := ioutil.ReadAll(resp.Body); err == nil {
69 u = &user.User{}
70 c := &credentials{}
71 if err = json.Unmarshal(body, &c); err != nil {
72 return nil, errors.New("(oauth) JSON Unmarshal: " + err.Error())
73 } else {
74 if c.Uname == "" {
75 return nil, errors.New("(oauth) " + e.InvalidAuth)
76 }
77 u.Username = c.Uname
78 if c.Name != "" {
79 u.Fullname = c.Name
80 } else {
81 u.Fullname = c.Fname + " " + c.Lname
82 }
83 u.Email = c.Email
84 if err = u.SetMongoInfo(); err != nil {
85 return nil, errors.New("(oauth) MongoDB: " + err.Error())
86 }
87 }
88 }
89 } else if resp.StatusCode == http.StatusForbidden {
90 return nil, errors.New("(oauth) " + e.InvalidAuth)
91 } else {
92 return nil, errors.New("(oauth) Authentication failed: Unexpected response status: " + resp.Status)
93 }
94 } else {
95 return nil, errors.New("(oauth) " + err.Error())
96 }
97 return
98}

Callers 1

AuthFunction · 0.85

Calls 4

SetMongoInfoMethod · 0.80
CloseMethod · 0.65
ErrorMethod · 0.45
AddMethod · 0.45

Tested by

no test coverage detected