sf-onedrive 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 84af99dd9e50ebec94fa4a920a74e3aba874b54d
4
+ data.tar.gz: 3777921720de70566bc7776f7dfffb4d7a1fa70a
5
+ SHA512:
6
+ metadata.gz: 16f94908d2c85a1fff368e02c25cdd5acbaa89eab3579260f4bbc620b48092f67c3a4df19d1835f1661cd941fed7a44690e32660797431c0025eb661319826eb
7
+ data.tar.gz: 196c51b43a1035a83a72cfd64643430b82484976a036323d03a964a72c9f3a78e5fa12f08aab346b26d3b91b881351ec77304bd775af690fef569fc13f566c16
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2017
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # Onedrive
2
+ Short description and motivation.
3
+
4
+ ## Usage
5
+ How to use my plugin.
6
+
7
+ ## Installation
8
+ Add this line to your application's Gemfile:
9
+
10
+ ```ruby
11
+ gem 'onedrive'
12
+ ```
13
+
14
+ And then execute:
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ Or install it yourself as:
20
+ ```bash
21
+ $ gem install onedrive
22
+ ```
23
+
24
+ ## Contributing
25
+ Contribution directions go here.
26
+
27
+ ## License
28
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,37 @@
1
+ begin
2
+ require 'bundler/setup'
3
+ rescue LoadError
4
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
5
+ end
6
+
7
+ require 'rdoc/task'
8
+
9
+ RDoc::Task.new(:rdoc) do |rdoc|
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = 'Onedrive'
12
+ rdoc.options << '--line-numbers'
13
+ rdoc.rdoc_files.include('README.md')
14
+ rdoc.rdoc_files.include('lib/**/*.rb')
15
+ end
16
+
17
+ APP_RAKEFILE = File.expand_path("../test/dummy/Rakefile", __FILE__)
18
+ load 'rails/tasks/engine.rake'
19
+
20
+
21
+ load 'rails/tasks/statistics.rake'
22
+
23
+
24
+
25
+ require 'bundler/gem_tasks'
26
+
27
+ require 'rake/testtask'
28
+
29
+ Rake::TestTask.new(:test) do |t|
30
+ t.libs << 'lib'
31
+ t.libs << 'test'
32
+ t.pattern = 'test/**/*_test.rb'
33
+ t.verbose = false
34
+ end
35
+
36
+
37
+ task default: :test
@@ -0,0 +1,2 @@
1
+ //= link_directory ../javascripts/onedrive .js
2
+ //= link_directory ../stylesheets/onedrive .css
@@ -0,0 +1,13 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file. JavaScript code in this file should be added after the last require_* statement.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,34 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS
10
+ * files in this directory. Styles in this file should be added after the last require_* statement.
11
+ * It is generally better to create a new file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
16
+
17
+ body, html {
18
+ font-family: sans-serif;
19
+ margin: 20px;
20
+ -webkit-font-smoothing: antialiased;
21
+ }
22
+
23
+ h1 {
24
+ margin-top: 2rem;
25
+ margin-bottom: 1rem;
26
+ font-weight: normal;
27
+ border-bottom: 1px solid black;
28
+ font-size: 2em;
29
+ margin: 0.67em 0;
30
+ }
31
+
32
+ h4 {
33
+ font-weight: bold !important;
34
+ }
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,5 @@
1
+ module Onedrive
2
+ class ApplicationController < ActionController::Base
3
+ protect_from_forgery with: :null_session
4
+ end
5
+ end
@@ -0,0 +1,130 @@
1
+ require_dependency "onedrive/application_controller"
2
+
3
+ module Onedrive
4
+ class DriveController < ApplicationController
5
+
6
+ require 'net/http'
7
+ require 'uri'
8
+
9
+ def onedrive_redirect
10
+ Onedrive.mount_url + "/auth_redirect"
11
+ end
12
+
13
+ def onedrive_link
14
+ permissions = "Files.ReadWrite.AppFolder Files.ReadWrite.All offline_access"
15
+ @onedrive_link = "https://login.microsoftonline.com/common/oauth2/v2.0/authorize?client_id=#{Onedrive.client_id}&scope=#{permissions}&response_type=code&redirect_uri=#{onedrive_redirect}"
16
+ end
17
+
18
+ def index
19
+ session["redirect_url"] = params[:redirect_url]
20
+ @onedrive_link = onedrive_link
21
+ redirect_to @onedrive_link
22
+ end
23
+
24
+ def auth_complete
25
+ code = params[:code]
26
+
27
+ url = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
28
+ params = {
29
+ :client_id => Onedrive.client_id,
30
+ :redirect_uri => onedrive_redirect,
31
+ :client_secret => Onedrive.client_secret,
32
+ :code => code,
33
+ :grant_type => "authorization_code"
34
+ }
35
+
36
+ uri = URI.parse(url)
37
+ http = Net::HTTP.new(uri.host, uri.port)
38
+ req = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/x-www-form-urlencoded')
39
+ req.set_form_data(params)
40
+ http.use_ssl = (uri.scheme == "https")
41
+ response = http.request(req)
42
+
43
+ if response.code[0] != '2'
44
+ redirect_to "#{Onedrive.mount_url}/?secret_url=error"
45
+ else
46
+ user = User.new
47
+ auth_params = JSON.parse(response.body)
48
+ user.expires_at = DateTime.now + auth_params["expires_in"].to_i.seconds
49
+ user.auth_params = auth_params.to_json
50
+ user.token = SecureRandom.uuid
51
+ user.save
52
+
53
+ @secret_url = CGI.escape("#{Onedrive.mount_url}/sync?key=#{user.token}&type=sf")
54
+ redirect_to "#{session["redirect_url"]}&onedrive_secret_url=#{@secret_url}"
55
+ end
56
+ end
57
+
58
+ def refresh_token(user, token)
59
+ url = "https://login.microsoftonline.com/common/oauth2/v2.0/token"
60
+ params = {
61
+ :client_id => Onedrive.client_id,
62
+ :redirect_uri => onedrive_redirect,
63
+ :client_secret => Onedrive.client_secret,
64
+ :refresh_token => token,
65
+ :grant_type => "refresh_token"
66
+ }
67
+
68
+ uri = URI.parse(url)
69
+ http = Net::HTTP.new(uri.host, uri.port)
70
+ req = Net::HTTP::Post.new(uri.request_uri, 'Content-Type' => 'application/x-www-form-urlencoded')
71
+ req.set_form_data(params)
72
+ http.use_ssl = (uri.scheme == "https")
73
+ response = http.request(req)
74
+
75
+ if response.code[0] != '2'
76
+ return nil
77
+ else
78
+ auth_params = JSON.parse(response.body)
79
+ user.expires_at = DateTime.now + auth_params["expires_in"].to_i.seconds
80
+ user.auth_params = auth_params.to_json
81
+ user.save
82
+ end
83
+ end
84
+
85
+ def sync
86
+ user = User.find_by_token(params[:key])
87
+ if !user
88
+ puts "Error: Unable to find user"
89
+ render :json => {:success => false}
90
+ return
91
+ end
92
+
93
+ onedrive_params = JSON.parse(user.auth_params)
94
+
95
+ if user.expires_at.past?
96
+ response = refresh_token(user, onedrive_params["refresh_token"])
97
+ if !response
98
+ puts "Error: unable to refresh token"
99
+ render :json => {:success => false}
100
+ return
101
+ end
102
+ end
103
+
104
+ items = params[:items]
105
+ auth_params = params[:auth_params]
106
+
107
+ if items && items.length > 0
108
+ items.each do |item|
109
+ payload = { "items" => [item]}
110
+ payload["auth_params"] = auth_params if auth_params != nil
111
+ upload_file(onedrive_params["access_token"], "#{item[:content_type]}-#{item[:uuid]}.txt", "#{JSON.pretty_generate(payload.as_json)}")
112
+ end
113
+ end
114
+
115
+ render :json => {:success => true}
116
+ end
117
+
118
+ def upload_file(access_token, name, body)
119
+ url = "https://graph.microsoft.com/v1.0/me/drive/special/approot:/#{name}:/content"
120
+ uri = URI.parse(url)
121
+ http = Net::HTTP.new(uri.host, uri.port)
122
+ req = Net::HTTP::Put.new(uri.request_uri, {'Authorization' => "Bearer #{access_token}", 'Content-Type' => 'application/json'})
123
+ req.body = body
124
+ http.use_ssl = (uri.scheme == "https")
125
+ response = http.request(req)
126
+ end
127
+
128
+
129
+ end
130
+ end
@@ -0,0 +1,4 @@
1
+ module Onedrive
2
+ module ApplicationHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Onedrive
2
+ module DriveHelper
3
+ end
4
+ end
@@ -0,0 +1,4 @@
1
+ module Onedrive
2
+ class ApplicationJob < ActiveJob::Base
3
+ end
4
+ end
@@ -0,0 +1,6 @@
1
+ module Onedrive
2
+ class ApplicationMailer < ActionMailer::Base
3
+ default from: 'from@example.com'
4
+ layout 'mailer'
5
+ end
6
+ end
@@ -0,0 +1,5 @@
1
+ module Onedrive
2
+ class ApplicationRecord < ActiveRecord::Base
3
+ self.abstract_class = true
4
+ end
5
+ end
@@ -0,0 +1,5 @@
1
+ module Onedrive
2
+ class User < ApplicationRecord
3
+
4
+ end
5
+ end
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>OneDrive Extension</title>
5
+ <%= stylesheet_link_tag "onedrive/application", media: "all" %>
6
+ <%= javascript_include_tag "onedrive/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,25 @@
1
+ <h1> OneDrive Data Backup </h1>
2
+
3
+ <p> This Standard File sync adapter creates real-time backups of your data to your OneDrive.</p>
4
+
5
+ <% if @secret_url %>
6
+ <p> Your secret URL is: <strong><%= @secret_url %></strong></p>
7
+
8
+ <p>
9
+ <strong>To register this sync adapter using your secret URL:</strong>
10
+ </p>
11
+
12
+ <ol>
13
+ <li>In the Standard Notes app, find and click "Account" in the lower left corner.</li>
14
+ <li>Click "Standard File Dashboard".</li>
15
+ <li>After logging in, click "Sync Adapters" in the left panel.</li>
16
+ <li>In the add new section, paste in the secret URL, and submit.</li>
17
+ </ol>
18
+
19
+ <p>That's it! Now, whenever you make changes to a note or tag, it will automatically be synced to your OneDrive.<p>
20
+
21
+ <% elsif @error %>
22
+ <strong style="color: red;">Error:</strong> <%= @error %>
23
+ <% else %>
24
+ <a href="<%= @onedrive_link %>">Link OneDrive</a>
25
+ <% end %>
data/config/routes.rb ADDED
@@ -0,0 +1,8 @@
1
+ Onedrive::Engine.routes.draw do
2
+
3
+ post "sync" => "drive#sync"
4
+
5
+ get "auth_redirect" => "drive#auth_complete"
6
+
7
+ root "drive#index"
8
+ end
@@ -0,0 +1,12 @@
1
+ class CreateOnedriveUsers < ActiveRecord::Migration[5.0]
2
+ def change
3
+ create_table :onedrive_users do |t|
4
+ t.string :token
5
+ t.text :auth_params
6
+ t.datetime :expires_at
7
+ t.timestamps
8
+ end
9
+
10
+ add_index :onedrive_users, :token
11
+ end
12
+ end
@@ -0,0 +1,5 @@
1
+ module Onedrive
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace Onedrive
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ module Onedrive
2
+ VERSION = '0.0.1'
3
+ end
data/lib/onedrive.rb ADDED
@@ -0,0 +1,7 @@
1
+ require "onedrive/engine"
2
+
3
+ module Onedrive
4
+ mattr_accessor :mount_url
5
+ mattr_accessor :client_id
6
+ mattr_accessor :client_secret
7
+ end
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :onedrive do
3
+ # # Task goes here
4
+ # end
metadata ADDED
@@ -0,0 +1,82 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: sf-onedrive
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - "@StandardNotes"
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-09-03 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 5.0.1
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 5.0.1
27
+ description: OneDrive Extension for Standard File/Standard Notes.
28
+ email:
29
+ - mo@standardnotes.org
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - MIT-LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - app/assets/config/onedrive_manifest.js
38
+ - app/assets/javascripts/onedrive/application.js
39
+ - app/assets/javascripts/onedrive/drive.js
40
+ - app/assets/stylesheets/onedrive/application.css
41
+ - app/assets/stylesheets/onedrive/drive.css
42
+ - app/controllers/onedrive/application_controller.rb
43
+ - app/controllers/onedrive/drive_controller.rb
44
+ - app/helpers/onedrive/application_helper.rb
45
+ - app/helpers/onedrive/drive_helper.rb
46
+ - app/jobs/onedrive/application_job.rb
47
+ - app/mailers/onedrive/application_mailer.rb
48
+ - app/models/onedrive/application_record.rb
49
+ - app/models/onedrive/user.rb
50
+ - app/views/layouts/onedrive/application.html.erb
51
+ - app/views/onedrive/drive/index.html.erb
52
+ - config/routes.rb
53
+ - db/migrate/20170903162159_create_onedrive_users.rb
54
+ - lib/onedrive.rb
55
+ - lib/onedrive/engine.rb
56
+ - lib/onedrive/version.rb
57
+ - lib/tasks/onedrive_tasks.rake
58
+ homepage: https://standardnotes.org
59
+ licenses:
60
+ - GPLv3
61
+ metadata: {}
62
+ post_install_message:
63
+ rdoc_options: []
64
+ require_paths:
65
+ - lib
66
+ required_ruby_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ required_rubygems_version: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ requirements: []
77
+ rubyforge_project:
78
+ rubygems_version: 2.6.12
79
+ signing_key:
80
+ specification_version: 4
81
+ summary: OneDrive Extension for Standard File/Standard Notes.
82
+ test_files: []