python -m SimpleHTTPServer <port>
# csr.conf # you can rename scr.conf, scr.key, scr.csr to filenames you prefer, # but with the same filename extensions [req] default_bits = 2048 # default_md = sha512 default_keyfile = csr.key #name of keyfile distinguished_name = req_distinguished_name prompt = yes # If 'prompt = no' provide right values to properties, not to _default properties # and remove or comment _default properties. # Use 'yes' and _default to see and correct values interactively encrypt_key = no # # req_extensions = v3_req #this is for multi-domain certificate [req_distinguished_name] # # Use your company name, e-mai, domain names as default values # if you enter '.', the field will be left blank # countryName = Country Name (2 letter code) countryName_default = GB stateOrProvinceName = State or Province Name (full name) stateOrProvinceName_default = City of London localityName = Locality Name (eg, city) localityName_default = London organizationName = Organization name organizationName_default = MyCompany Limited organizationalUnitName = Organizational Unit Name (eg, section) organizationalUnitName_default = . commonName = This is fully qualified domain name that you wish to secure # eg www.example.com or mail.example.com commonName_default = www.my-domain.com emailAddress = Email Address emailAddress_default = admin@my-domain.com # [v3_req] # subjectAltName = @alt_names #this is for multi-domain certificate # [alt_names] #this is for multi-domain certificate # DNS.1 = my-domain.net #this is for multi-domain certificate # DNS.2 = my-domain.org #this is for multi-domain certificate # DNS.3 = myseconddomain.com #this is for multi-domain certificate # to generate .csr: # openssl req -newkey rsa:2048 -sha512 -out csr.csr -config csr.conf # or: # openssl req -newkey rsa:2048 -sha512 -nodes -out csr.csr -config csr.conf # Note: If the "-nodes" is entered the key will NOT be encrypted with a # DES pass phrase, ( see: # https://support.comodo.com/index.php?/Default/Knowledgebase/Article/View/1/19/csr-generation-using-openssl-apache-wmod_ssl-nginx-os-x # ) # # to verify .csr: # openssl req -text -noout -verify -in csr.csr # # in one command generate and verify: # openssl req -newkey rsa:2048 -out csr.csr -config csr.conf && openssl req -text -noout -verify -in csr.csr #
openssl req -newkey rsa:2048 -out csr.csr -config csr.conf
openssl req -text -noout -verify -in csr.csr
-----BEGIN CERTIFICATE REQUEST----- MIIC5TCCAc0CAQAwgZ8xCzAJBgNVBAYTAkdCMRcwFQYDVQQIDA5DaXR5IG9mIExv bmRvbjEPMA0GA1UEBwwGTG9uZG9uMRowGAYDVQQKDBFNeUNvbXBhbnkgTGltaXRl NZBB4bDdgJ+uyNZq54dM1tUvzSolv/+LAY78/z85edqLH4nc5CxgMEn8hurFOpB4 RXS+ShhpBsJr6RJhSk2xkRe/idkM/TUon/7n1TUthFpjv2tYQZ6on3iWUZ61FDuM mNPHGMIX+sn/OceViRtlu1Lx+t4JV9dTJQ== -----END CERTIFICATE REQUEST-----
openssl x509 -in { }.crt -text -noout
openssl rsa -in *.key > forGAE.key.pem
cat mydomain_com.crt ASecureServerCA.crt ATrustCA.crt ATrustExternal.crt > concat.crt
openssl x509 -noout -modulus -in concat.crt | openssl md5
openssl rsa -noout -modulus -in forGAE.key.pem | openssl md5
mvn archetype:generate -Dappengine-version=1.9.28 -Dapplication-id=hello-habrahabr-webapp -Dfilter=com.google.appengine.archetypes:
1: remote -> com.google.appengine.archetypes:appengine-skeleton-archetype (A skeleton application with Google App Engine)
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <!-- Force SSL for entire site --> <!-- (server automatically redirects the requests to the SSL port if you try to use HTTP ) --> <security-constraint> <web-resource-collection> <web-resource-name>Entire Application</web-resource-name> <url-pattern>/*</url-pattern> </web-resource-collection> <user-data-constraint> <transport-guarantee>CONFIDENTIAL</transport-guarantee> </user-data-constraint> </security-constraint> <!-- Welcome file list --> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list> <!-- --> </web-app>
git init gcloud init git config credential.helper gcloud.sh git remote add google https://source.developers.google.com/p/hello-habrahabr-webapp git push --all google set.account.sh commit.push.build.and.upload.sh
gcloud components update app-engine-java
export JAVA_HOME=/usr/lib/jvm/java-7-oracle/ # Java 7 ( ) export PATH=$JAVA_HOME/bin:$PATH
source ~/.bashrc
mvn clean install
mvn gcloud:run
package com.appspot.hello_habrahabr_api; import com.google.api.server.spi.config.Api; import com.google.api.server.spi.config.ApiMethod; import com.google.api.server.spi.config.ApiMethod.HttpMethod; import java.util.logging.Logger; import java.util.Random; @Api(name = "myApi", //The api name must match '[az]+[A-Za-z0-9]*' version = "v1", scopes = {Constants.EMAIL_SCOPE}, description = "first API for this application.") public class YourFirstAPI { @SuppressWarnings("unused") private static final Logger LOG = Logger.getLogger(YourFirstAPI.class.getName()); @ApiMethod( name = "registerPOST", path = "register", httpMethod = HttpMethod.POST ) @SuppressWarnings("unused") public MessageToUser registerPOST(final UserForm userForm) { LOG.warning("[YourFirstAPI] (HTTP Method: POST): userForm: {name: " + userForm.getName() + ", age: " + userForm.getAge() + ", ishuman: " + userForm.getIshuman() + "}" ); MessageToUser messageToUser = new MessageToUser(); messageToUser.setMessage("Hi, " + userForm.getName() + ", you are registered on our site"); Random random = new Random(); messageToUser.setUsernumber(random.nextInt(100) + 1); messageToUser.setIsregistered(true); return messageToUser;} @ApiMethod( name = "getGreeting", path = "getgreeting", httpMethod = HttpMethod.GET ) @SuppressWarnings("unused") public MessageToUser getGreeting(final UserForm userForm) { LOG.warning("[YourFirstAPI] (HTTP Method: GET): userForm: {name: " + userForm.getName() + ", age: " + userForm.getAge() + ", ishuman: " + userForm.getIshuman() + "}" ); MessageToUser messageToUser = new MessageToUser(); messageToUser.setMessage("Hi, " + userForm.getName() + ", nice to meet you :)"); Random random = new Random(); messageToUser.setUsernumber(random.nextInt(100) + 1); messageToUser.setIsregistered(false); return messageToUser;} }
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <link rel="shortcut icon" href="favicon.ico?" /> <!-- JQUERY 2.1.4 --> <script src="vendors/jquery-2.1.4.js"></script> <!-- BOOTSTRAP 3.3.5 --> <link rel="stylesheet" href="vendors/bootstrap.css"> <link rel="stylesheet" href="vendors/bootstrap-theme.css"> <script src="vendors/bootstrap.js"></script> <!-- ANGULARJS 1.4.7 --> <script src="vendors/angular.js"></script> <script src="vendors/angular-route.js"></script> <!-- <script src="https://code.jquery.com/jquery-2.1.4.min.js"></script> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css"> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css"> <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.7/angular.min.js"></script> <script src="https://code.angularjs.org/1.4.7/angular-route.min.js"></script> --> <!-- angular-google-gapi --> <!-- https://github.com/maximepvrt/angular-google-gapi/releases --> <script src="vendors/angular-google-gapi.js"></script> <!-- ngProgress --> <!-- https://github.com/VictorBjelkholm/ngProgress --> <script src="vendors/ngprogress.js"></script> <link rel="stylesheet" href="vendors/ngProgress.css"> <!-- angular-google-gapi --> <!-- https://github.com/maximepvrt/angular-google-gapi/releases --> <script src="vendors/angular-google-gapi.js"></script> <!-- ~~~ MY APP ~~~ --> <!-- main app module --> <script src="js/app.js"></script> <!-- controllers --> <script src="js/simpleformcontroller.js"></script> <script src="js/authformcontroller.js"></script> <!-- --> <title>Cloud Endpoints Frontend</title> </head> <body ng-app="myApp"> <ng-view></ng-view> </body> </html>
'use strict'; ( function () { // create main module var app = angular.module("myApp", [ 'ngRoute', // https://code.angularjs.org/1.4.7/docs/api/ngRoute 'ngProgress' // https://github.com/victorbjelkholm/ngprogress ] ); // routes app.config(function ($routeProvider) { $routeProvider.when('/', { templateUrl: "templates/simpleform.html", controller: "SimpleFormController" }).when('/auth/', { templateUrl: "templates/authform.html", controller: "AuthFormController" }) .otherwise({redirectTo: '/'}) } ); }() );
'use strict'; ( function() { var SimpleFormController = function($scope, $http, ngProgressFactory) { $scope.progressbar = ngProgressFactory.createInstance(); $scope.progressbar.setColor('blue'); $scope.data = {}; // data from form (ng-model="data. ...") // $http configuration object // https://code.angularjs.org/1.4.7/docs/api/ng/service/$http#usage $scope.postReq = { method: 'POST', url: 'https://hello-habrahabr-api.appspot.com/_ah/api/myApi/v1/register', data: $scope.data // 'data' for POST }; $scope.getReq = { method: 'GET', url: 'https://hello-habrahabr-api.appspot.com/_ah/api/myApi/v1/getgreeting', params: $scope.data // 'params:' for GET }; $scope.actionPost = function() { $scope.progressbar.start(); console.log("request sent:"); // for testing console.log($scope.postReq); // for testing // AJAX request: // $http(req).then(function(){...}, function(){...}); $http($scope.postReq).then(function(response) { // success: $scope.progressbar.complete(); $scope.serverresponse = response; $scope.errormessage = null; console.log("response received:"); console.log(response); }, // comma operator, see: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Comma_Operator function(response) { // error: $scope.progressbar.complete(); $scope.errormessage = response; $scope.serverresponse = null; console.log("get Error form server:"); console.log(response); }); }; $scope.actionGet = function() { $scope.progressbar.start(); console.log("request sent:"); // for testing console.log($scope.getReq); // for testing // AJAX request: // $http(req).then(function(){...}, function(){...}); $http($scope.getReq).then(function(response) { // success: $scope.progressbar.complete(); $scope.serverresponse = response; $scope.errormessage = null; console.log("response received:"); console.log(response); }, // comma operator, see: https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Operators/Comma_Operator function(response) { // error: $scope.progressbar.complete(); $scope.errormessage = response; $scope.serverresponse = null; console.log("get Error form server:"); console.log(response); }); }; $scope.clear = function() { $scope.serverresponse = null; $scope.errormessage = null; $scope.data = null; }; }; // $inject property annotation // see: https://code.angularjs.org/1.4.7/docs/guide/di SimpleFormController.$inject = ['$scope', '$http', 'ngProgressFactory']; angular.module('myApp') .controller('SimpleFormController', SimpleFormController); }());
<div class="container"> <!-- Navigation Bar --> <nav class="navbar navbar-inverse"> <a class="navbar-brand" href="#/">Front-end for Cloud Endpoints API</a> <ul class="nav navbar-nav"> <li class="active"><a href="#/">Home</a></li> <li><a href="#/auth/">Auth</a></li> </ul> </nav> <!-- Form --> <form> <fieldset> <legend style="font-weight: bold;">Submit your data to server</legend> <p> <label> Name: <input ng-model="data.name"> </label> </p> <label> Age: <p> <input type="range" min="0" max="120" step="1" ng-model="data.age"> </p> <p> <input ng-model="data.age" name="ageNumber" class="col-md-3"> <span class="col-md-5">years old</span> </p> </label> <br> <label> <p> Is a human: <input type="checkbox" ng-model="data.ishuman" name="data.ishuman"> </p> </label> </fieldset> <input type="button" value="Submit GET" ng-click="actionGet()"> <input type="button" value="Submit POST" ng-click="actionPost()"> <input type="reset" ng-click="clear()"> </form> <!-- Server Response --> <br> <div class="panel panel-default"> <div ng-hide="(serverresponse != null || errormessage != null)" class="panel-body"> <p></p> <br> <p></p> <br> </div> <div ng-show="serverresponse != null" class="panel-body"> <p>Response from server: </p> <p> {{ serverresponse.data.message }} and your number is: {{serverresponse.data.usernumber}} </p> </div> <div ng-show="errormessage != null" class="panel-body"> <p>Error from server: </p> <p> {{errormessage.data.error.message}} </p> </div> </div> </div>
python -m SimpleHTTPServer
package com.appspot.hello_habrahabr_api; import com.google.api.server.spi.Constant; /** * Contains the client IDs and scopes for allowed clients consuming your API. */ public class Constants { public static final String WEB_CLIENT_ID = "647700180043-m8et0au4vhgiv2n4iqr2hssn0mkkl7q0.apps.googleusercontent.com"; public static final String ANDROID_CLIENT_ID = "replace this with your Android client ID"; public static final String IOS_CLIENT_ID = "replace this with your iOS client ID"; public static final String ANDROID_AUDIENCE = WEB_CLIENT_ID; public static final String EMAIL_SCOPE = "https://www.googleapis.com/auth/userinfo.email"; public static final String API_EXPLORER_CLIENT_ID = Constant.API_EXPLORER_CLIENT_ID; }
scopes = {Constants.EMAIL_SCOPE}, clientIds = {Constants.WEB_CLIENT_ID, Constants.API_EXPLORER_CLIENT_ID},
package com.appspot.hello_habrahabr_api; /** * explore on: https://apis-explorer.appspot.com/apis-explorer/?base=https://hello-habrahabr-api.appspot.com/_ah/api#p/oAuth2Api/v1/ */ import com.google.api.server.spi.config.Api; import com.google.api.server.spi.config.ApiMethod; import com.google.api.server.spi.config.ApiMethod.HttpMethod; import com.google.api.server.spi.response.UnauthorizedException; import com.google.appengine.api.users.User; import com.google.appengine.repackaged.com.google.gson.Gson; import java.util.Random; import java.util.logging.Logger; @Api(name = "oAuth2Api", // The api name must match '[az]+[A-Za-z0-9]*' version = "v1", scopes = {Constants.EMAIL_SCOPE}, clientIds = {Constants.WEB_CLIENT_ID, Constants.API_EXPLORER_CLIENT_ID}, description = "API using OAuth2") public class OAuth2Api { @SuppressWarnings("unused") private static final Logger LOG = Logger.getLogger(OAuth2Api.class.getName()); @ApiMethod( name = "getUserInfo", path = "getuserinfo", httpMethod = HttpMethod.POST ) @SuppressWarnings("unused") public MessageToUser getUserInfo(final User user, final UserForm userForm) throws UnauthorizedException { if (user == null) { LOG.warning("User not logged in"); throw new UnauthorizedException("Authorization required"); } MessageToUser messageToUser = new MessageToUser(); Gson gson = new Gson(); messageToUser.setMessage( "Hi, " + userForm.getName() + ", your data from Google: " + gson.toJson(user) ); Random random = new Random(); messageToUser.setUsernumber(random.nextInt(100) + 1); messageToUser.setIsregistered(true); return messageToUser; } }
<script src="vendors/angular-google-gapi.js"></script>
'use strict'; ( function () { // create main module var app = angular.module("myApp", [ 'ngRoute', // https://code.angularjs.org/1.4.7/docs/api/ngRoute 'ngProgress', // https://github.com/victorbjelkholm/ngprogress 'angular-google-gapi' // https://github.com/maximepvrt/angular-google-gapi/ - add app.run() after app.config() ]); // routes app.config(function ($routeProvider) { $routeProvider.when('/', { templateUrl: "templates/simpleform.html", controller: "SimpleFormController" }).when('/auth/', { templateUrl: "templates/authform.html", controller: "AuthFormController" }).otherwise({redirectTo: '/'}) }); app.run(['GAuth', 'GApi', 'GData', '$rootScope', function (GAuth, GApi, GData, $rootScope) { $rootScope.gdata = GData; // var CLIENT = '647700180043-m8et0au4vhgiv2n4iqr2hssn0mkkl7q0.apps.googleusercontent.com'; var BASE = 'https://hello-habrahabr-api.appspot.com/_ah/api'; GApi.load('oAuth2Api', 'v1', BASE); GAuth.setClient(CLIENT); // see: https://github.com/maximepvrt/angular-google-gapi/issues/8 GAuth.setScope("https://www.googleapis.com/auth/userinfo.email"); GAuth.checkAuth().then( function () { // action if it's possible to authenticate user at startup of the application console.log("user authenticated, $rootScope.gapi.user: "); console.log(console.log($rootScope.gapi.user)); }, function () { // action if it's impossible to authenticate user at startup of the application console.log("user not authenticated, $rootScope.gapi.user: "); console.log(console.log($rootScope.gapi.user)); } ); $rootScope.login = function () { // shows auth window from Google GAuth.login().then( function () { console.log('user logged in'); console.log(console.log($rootScope.gapi.user)); }); }; /* * As stated on * https://cloud.google.com/appengine/docs/java/endpoints/consume_js: * "There is no concept of signing out using the JS client library, * but you can emulate the behavior if you wish by any of these methods: * - Resetting the UI to the pre signed-in state. * - Setting any signed-in variables in your application logic to false * - Calling gapi.auth.setToken(null);" // <- * * */ $rootScope.logout = function () { GAuth.logout().then( function () { console.log('user logged out'); console.log(console.log($rootScope.gapi.user)); }); }; } ]); }() );
'use strict'; ( function () { var AuthFormController = function ($scope, $rootScope, $http, GApi, GAuth, ngProgressFactory) { $scope.progressbar = ngProgressFactory.createInstance(); $scope.progressbar.setColor('#00F'); $scope.data = {}; // object to store form data $scope.authrequest = function () { $scope.progressbar.start(); GAuth.checkAuth().then( function () { // if it's possible to authenticate user GApi.executeAuth( // execute request 'oAuth2Api', 'getUserInfo', $scope.data // data to send to server ) .then(function (resp) { // receive response $scope.userinfofromserver = resp; console.log("GApi.executeAuth() get resp:"); console.log(resp); $scope.progressbar.complete(); }); }, function () { // if it's impossible to authenticate user $scope.userinfofromserver = {}; $scope.userinfofromserver.message = 'User not logged in '; console.log("GAuth.checkAuth() - failed: "); console.log(console.log($rootScope.gapi.user)); $scope.progressbar.complete(); } ); }; $scope.clear = function () { $scope.userinfofromserver = null; $scope.data = null; }; }; // $inject property annotation // see: https://code.angularjs.org/1.4.7/docs/guide/di AuthFormController.$inject = ['$scope', '$rootScope', '$http', 'GApi', 'GAuth', 'ngProgressFactory']; angular.module('myApp') .controller('AuthFormController', AuthFormController); }() );
<div class="container"> <!-- Navigation Bar --> <nav class="navbar navbar-inverse"> <a class="navbar-brand" href="#/">Front-end for Cloud Endpoints API</a> <ul class="nav navbar-nav"> <li><a href="#/">Home</a></li> <li class="active"><a href="#/auth/">Auth</a></li> </ul> <!-- --> <ul class="nav navbar-nav navbar-right"> <li> <a ng-show="gapi.user" href="" class="navbar-brand" style="padding-top: 0;"> <img src="{{gapi.user.picture}}" class="navbar-brand" style="padding-top: 0; padding-bottom: 0;"></a> </li> </ul> <!-- --> <ul class="nav navbar-nav navbar-right"> <!-- --> <li ng-show="!gapi.user" class="active"> <a href="" ng-click="login()">login</a> </li> <!-- --> <li ng-show="gapi.user"> <a href="">{{gapi.user.email}}</a> </li> <li ng-show="gapi.user"> <a href="" ng-click="logout()">logout</a> </li> </ul> </nav> <!-- Form --> <form> <fieldset> <legend style="font-weight: bold;">Submit your request to server</legend> <p> <label> Your name: <input ng-model="data.name"> </label> </p> </fieldset> <input type="button" value="Auth request" ng-click="authrequest()"> <input type="reset" ng-click="clear()"> </form> <!-- Server Response --> <br> <div class="panel panel-default" style="font-weight: bold;" > <!-- --> <div ng-hide="(userinfofromserver != null)" class="panel-body"> <p></p> <br> <p></p> <br> </div> <!-- --> <div ng-show="userinfofromserver" class="panel-body" style=""> <p> User's data from server (reg # {{userinfofromserver.usernumber}}): </p> <p>{{userinfofromserver.message}}</p> </div> <!-- --> </div> </div>
Source: https://habr.com/ru/post/270459/
All Articles