/*
 * Copyright 2000-2012 JetBrains s.r.o.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jetbrains.plugins.github;

import com.intellij.openapi.util.text.StringUtil;
import com.intellij.util.AuthData;
import git4idea.jgit.GitHttpAuthDataProvider;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

/**
 * @author Kirill Likhodedov
 */
public class GithubHttpAuthDataProvider implements GitHttpAuthDataProvider {

  @Nullable
  @Override
  public AuthData getAuthData(@NotNull String url) {
    if (!GithubUrlUtil.isGithubUrl(url)) {
      return null;
    }

    GithubSettings settings = GithubSettings.getInstance();
    if (settings.getAuthType() != GithubAuthData.AuthType.BASIC) {
      return null;
    }

    GithubAuthData.BasicAuth basic = settings.getAuthData().getBasicAuth();
    assert basic != null;

    if (StringUtil.isEmptyOrSpaces(basic.getLogin()) || StringUtil.isEmptyOrSpaces(basic.getPassword())) {
      return null;
    }

    return new AuthData(basic.getLogin(), basic.getPassword());
  }

}

@@ -1125,25 +1125,21 @@
 thub
-Settings settings
+AuthData auth
  = G
@@ -1169,67 +1169,52 @@
 ce()
-;%0A    if (settings.getAuthType() != GithubAuthData.
+.getAuthData();%0A    switch (auth.get
 AuthType
 .BAS
@@ -1213,14 +1213,10 @@
 Type
-.BASIC
+()
 ) %7B%0A
@@ -1221,36 +1221,32 @@
 %7B%0A      
-return null;
+case BASIC:
 %0A    
-%7D%0A%0A
     Gith
@@ -1275,33 +1275,19 @@
 asic
- = settings.getAuthData()
+Auth = auth
 .get
@@ -1303,16 +1303,20 @@
 ();%0A    
+  
+  
 assert b
@@ -1319,16 +1319,20 @@
 rt basic
+Auth
  != null
@@ -1333,17 +1333,20 @@
 = null;%0A
-%0A
+    
     if (
@@ -1373,24 +1373,28 @@
 Spaces(basic
+Auth
 .getLogin())
@@ -1425,24 +1425,28 @@
 Spaces(basic
+Auth
 .getPassword
@@ -1450,32 +1450,36 @@
 ord())) %7B%0A      
+   
+ 
 return null;%0A   
@@ -1475,27 +1475,34 @@
 n null;%0A    
-%7D%0A%0A
+    %7D%0A    
     return n
@@ -1514,24 +1514,28 @@
 thData(basic
+Auth
 .getLogin(),
@@ -1540,16 +1540,20 @@
 ), basic
+Auth
 .getPass
@@ -1561,15 +1561,321 @@
 ord());%0A
+      case TOKEN:%0A        GithubAuthData.TokenAuth tokenAuth = auth.getTokenAuth();%0A        assert tokenAuth != null;%0A        if (StringUtil.isEmptyOrSpaces(tokenAuth.getToken())) %7B%0A          return null;%0A        %7D%0A        return new AuthData(tokenAuth.getToken(), %22x-oauth-basic%22);%0A    %7D%0A    return null;%0A
   %7D%0A%0A%7D%0A

add Github -> git authentication with OAuth