2017-12-20 18 views
0

생성 :구글 중포 기지 및 API는 : 다트 구글 중포 기지와 구글 API를 사용하여 액세스 토큰 및 사용자 지정 액세스 토큰

  1. 가 생성 : .dart에서 다음과 같은 두 가지의 .js 프로그램에 해당하는이 있는가 서버의 액세스 토큰 :

;

var google = require("googleapis"); 

// Load the service account key JSON file. 
var serviceAccount = require("./xxxxxxxx.json"); 

// Define the required scopes. 
var scopes = [ 
    "https://www.googleapis.com/auth/userinfo.email", 
    "https://www.googleapis.com/auth/firebase.database" 
]; 

// Authenticate a JWT client with the service account. 
var jwtClient = new google.auth.JWT(
    serviceAccount.client_email, 
    null, 
    serviceAccount.private_key, 
    scopes 
); 

// Use the JWT client to generate an access token. 
jwtClient.authorize(function(error, tokens) { 
    if (error) { 
    console.log("Error making request to generate access token:", error); 
    } else if (tokens.access_token === null) { 
    console.log("Provided service account does not have permission to generate access tokens"); 
    } else { 
    var accessToken = tokens.access_token; 
    console.log(accessToken); 
    // See the "Using the access token" section below for information 
    // on how to use the access token to send authenticated requests to 
    // the Realtime Database REST API. 
    } 
}); 

2 :

서버의 사용자 액세스 토큰과 (테스트 여기 하나에 혼합) 클라이언트 측 생성 :

var google = require("googleapis"); 

var serviceAccount = require("xxxxxxxx.json"); 

var admin = require("firebase-admin"); 

admin.initializeApp({ 
    credential: admin.credential.cert(serviceAccount), 
    databaseURL: "xxxx" 
}); 

var firebase = require("firebase"); 


    var config = { 
    apiKey: "xxxx", 
    authDomain: "xxxxxx", 
    databaseURL: "xxxx", 
    projectId: "xxxx", 
    storageBucket: "xxx", 
    messagingSenderId: "xxxxx" 
    }; 

firebase.initializeApp(config); 


var uid = "some-uid"; 


var additionalClaims = { 
    premiumAccount: true 
}; 



admin.auth().createCustomToken(uid/*, additionalClaims*/) 
    .then(function(customToken) { 




    // begin client use, actually in browser. Here for testing. 

     firebase.auth().signInWithCustomToken(customToken).then(
     function(d){ 

      var fbref = firebase.database().ref('/users/'+uid ); 

       fbref.set({ 
       username: "name", 
       email: "email", 
       profile_picture : "imageUrl" 
       }).catch(function(error) { 
       console.log("Kann nicht setzen: " + error); 
      }); 

      var userId = firebase.auth().currentUser.uid; 

      return firebase.database().ref('/users/' + userId).once('value').then(function(snapshot) { 

      console.log(snapshot.val()); 
      }); 
     }  
    ).catch(function(error) { 
     var errorCode = error.code; 
     var errorMessage = error.message; 

    }); 

    // end client use 

    }) 
    .catch(function(error) { 
    console.log("Error creating custom token:", error); 
    }); 

내가 무엇을 알고 싶습니다을 필요한 패키지 및 문서를 얻을 수있는 곳입니다.

적어도 다트 프로그램으로의 개략적 인 번역이 도움이 될 것입니다. 당신이 원하는

답변

1

sulution은 토큰 서명에 RSA를 사용하는 jwt 라이브러리를 찾는 것이므로 RSA는 기존 라이브러리 중에서 덜 인기있는 것으로 보입니다.

pubspec.yaml :

dependencies: 
    firebase: "4.2.0+1" 
    just_jwt: "1.3.1" 

createCustomToken.dart :

import 'dart:async'; 
import 'dart:convert' as convert; 
import 'package:just_jwt/just_jwt.dart' as jj; 

Future main() async { 

    String client_email = "xxxxxxx"; 

    final DateTime issuedAt = new DateTime.now(); 
    final DateTime expiresAt = issuedAt.add(const Duration(minutes: 30)); 

    String uid = "some-uid"; 
    jj.EncodedJwt encodedJwt; 
    Map<String, Object> payload = { 
    "iss": client_email, 
    "sub": client_email, 
    "aud":  "xxxxxx", 
    "iat": (issuedAt.millisecondsSinceEpoch/1000).floor(), 
    "exp": (issuedAt.millisecondsSinceEpoch/1000).floor() + (60 * 30), 
    "uid": uid, 
    "claims": {} 
    }; 
    var sign = jj.toTokenSigner(jj.createRS256Signer("xxxxxxxxxx")); 
    Map<String, jj.TokenSigner> signer = {'RS256': sign}; 
    jj.Encoder encoder = new jj.Encoder(jj.composeTokenSigners(signer)); 
    jj.Jwt jwt = new jj.Jwt.RS256(payload); 
    encodedJwt = await encoder.convert(jwt); 
    print(encodedJwt.toString()); 
} 
1