github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/pkg/importer/picasa/auth.go (about)

     1  /*
     2  Copyright 2014 The Camlistore Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8       http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Package picasa is an importer for Picasa Web.
    18  package picasa
    19  
    20  import (
    21  	"encoding/json"
    22  	"errors"
    23  	"fmt"
    24  	"log"
    25  	"net/http"
    26  	"strings"
    27  
    28  	"camlistore.org/pkg/httputil"
    29  	"camlistore.org/third_party/code.google.com/p/goauth2/oauth"
    30  )
    31  
    32  const tokenAttrName = "picasaOAuth2Token"
    33  
    34  // PutToken saves the token into the root node.
    35  func (im *imp) PutToken(token *oauth.Token) error {
    36  	root, err := im.getRootNode()
    37  	if err != nil {
    38  		return err
    39  	}
    40  	encoded, err := json.Marshal(token)
    41  	if err != nil {
    42  		return err
    43  	}
    44  	if err := root.SetAttr(tokenAttrName, string(encoded)); err != nil {
    45  		return err
    46  	}
    47  	return nil
    48  }
    49  
    50  func (im *imp) Token() (*oauth.Token, error) {
    51  	root, err := im.getRootNode()
    52  	if err != nil {
    53  		return nil, err
    54  	}
    55  	encoded := root.Attr(tokenAttrName)
    56  	if encoded == "" {
    57  		return nil, errors.New("No OAuth2 token")
    58  	}
    59  	token := &oauth.Token{}
    60  	if err := json.Unmarshal([]byte(encoded), token); err != nil {
    61  		return nil, err
    62  	}
    63  	return token, nil
    64  }
    65  
    66  func (im *imp) ServeHTTP(w http.ResponseWriter, r *http.Request) {
    67  	if strings.HasSuffix(r.URL.Path, "/login") || strings.HasSuffix(r.URL.Path, "/start") {
    68  		im.serveLogin(w, r)
    69  	} else if strings.HasSuffix(r.URL.Path, "/callback") {
    70  		im.serveCallback(w, r)
    71  	} else {
    72  		httputil.BadRequestError(w, "Unexpected path: %s", r.URL.Path)
    73  	}
    74  }
    75  
    76  func (im *imp) serveLogin(w http.ResponseWriter, r *http.Request) {
    77  	im.Lock()
    78  	defer im.Unlock()
    79  	token, err := im.Token()
    80  	if err == nil {
    81  		im.transport.Token = token
    82  		http.Redirect(w, r, im.host.BaseURL+"?mode=start", 302)
    83  		return
    84  	}
    85  	im.transport.Config.RedirectURL = im.host.BaseURL + "callback"
    86  	authURL := im.transport.Config.AuthCodeURL("picago")
    87  	http.Redirect(w, r, authURL, 302)
    88  }
    89  
    90  func (im *imp) serveCallback(w http.ResponseWriter, r *http.Request) {
    91  	im.Lock()
    92  	defer im.Unlock()
    93  	token, err := im.transport.Exchange(r.FormValue("code"))
    94  	if err != nil {
    95  		http.Error(w, fmt.Sprintf("error exchanging code: %v", err), http.StatusBadRequest)
    96  		return
    97  	}
    98  	im.transport.Token = token
    99  	if err = im.PutToken(token); err != nil {
   100  		log.Printf("Picasa Importer serveCallback: %v", err)
   101  	}
   102  	http.Redirect(w, r, im.host.BaseURL+"?mode=start", 302)
   103  }