예제 #1
0
파일: auth.go 프로젝트: malston/pezauth
//DomainChecker - check the authenticated users domain to see if it is in the whitelist
func DomainChecker(res http.ResponseWriter, tokens oauth2.Tokens) {
	userInfo := GetUserInfo(tokens)

	if domain, ok := userInfo["domain"]; !ok || tokens.Expired() || isBlockedDomain(domain.(string)) {
		res.WriteHeader(FailureStatus)
		res.Write(AuthFailureResponse)
	}
}
예제 #2
0
/* Middleware to:
   - Retrieve data from github if user is logged in and data is not stored in sessions
   - Delete session data if user logged out */
func RequestData(tokens oauth2.Tokens, session sessions.Session) {
	if !tokens.Expired() && session.Get("username") == nil {
		access := tokens.Access()
		req, _ := http.NewRequest("GET", "https://api.github.com/user?access_token="+access, nil)
		client := &http.Client{}
		resp, _ := client.Do(req)

		body, _ := ioutil.ReadAll(resp.Body)

		parse := map[string]interface{}{}
		json.Unmarshal([]byte(string(body)), &parse)
		session.Set("username", parse["login"])
		session.Set("user_id", parse["id"])
		session.Set("avatar", parse["avatar_url"])

		log.Println("retrieved data")
	} else if tokens.Expired() && session.Get("username") != nil {
		session.Clear()
		log.Println("removed data")
	}
}