Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

chore: oauth & ip service debug#444

Merged
mydearxym merged 19 commits intodevfromoauth
Dec 11, 2021
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
19 commits
Select commitHold shift + click to select a range
03f972e
chore: add log
mydearxymDec 6, 2021
34d863b
chore(deploy): debug oauth
mydearxymDec 6, 2021
5a4a803
chore(deploy): debug oauth
mydearxymDec 6, 2021
9781f0c
chore(deploy): debug oauth
mydearxymDec 6, 2021
67b9a73
chore(deploy): debug oauth
mydearxymDec 6, 2021
3952ac0
chore(deploy): debug oauth
mydearxymDec 6, 2021
9c6877e
chore: fix & debug
mydearxymDec 8, 2021
32015bc
chore: bump version
mydearxymDec 8, 2021
b63b8ca
chore: debug ip
mydearxymDec 8, 2021
4be63cd
chore: fix ci errors
mydearxymDec 8, 2021
4a210a6
chore: add ip debug info
mydearxymDec 9, 2021
6c6a1d4
chore: bump version
mydearxymDec 9, 2021
12af6b2
chore: ip works, clean up
mydearxymDec 9, 2021
a6928cf
chore: bump version
mydearxymDec 9, 2021
a01e7eb
chore: fix some read issue when debug works
mydearxymDec 11, 2021
45e30b7
chore: bump version
mydearxymDec 11, 2021
e2393a6
fix(audit): token fail edege case
mydearxymDec 11, 2021
8b7725e
chore(deploy): bump version
mydearxymDec 11, 2021
8e1db2f
fix: audit key missing in test env
mydearxymDec 11, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 4 additions & 6 deletionsconfig/mock.exs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -17,12 +17,10 @@ config :phoenix, :stacktrace_depth, 20

# Configure your database
config :groupher_server, GroupherServer.Repo,
adapter: Ecto.Adapters.Postgres,

# username: "postgres",
# password: "postgres",
# database: "groupher_server_mock",
# hostname: "localhost",
username: "postgres",
password: "postgres",
database: "groupher_server_mock",
hostname: "localhost",
pool_size: 90

# config email services
Expand Down
1 change: 1 addition & 0 deletionsconfig/prod.exs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -91,6 +91,7 @@ config :groupher_server, :github_oauth,

config :groupher_server, :ip_locate, ip_service: System.get_env("IP_LOCATE_KEY")
config :groupher_server, :plausible, token: System.get_env("PLAUSIBLE_TOKEN")
config :groupher_server, :audit, token: System.get_env("AUDIT_TOKEN")

config :sentry,
dsn: System.get_env("SENTRY_DSN"),
Expand Down
3 changes: 3 additions & 0 deletionsconfig/test.exs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,3 +32,6 @@ config :groupher_server, :github_oauth,

# config email services
config :groupher_server, GroupherServer.Mailer, adapter: Bamboo.TestAdapter

config :groupher_server, :audit,
token: "24.aa6fb4e4018c371e9ed228db5bea3ec0.2592000.1641816180.282335-25148796"
Binary file modifieddeploy/production/api_server.tar.gz
View file
Open in desktop
Binary file not shown.
20 changes: 12 additions & 8 deletionslib/groupher_server/cms/delegates/article_curd.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -552,10 +552,22 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
|> result()
end

# pending article can be seen is viewer is author
defp check_article_pending(thread, id, %User{} = user) when is_atom(thread) do
with {:ok, info} <- match(thread),
{:ok, article} <- ORM.find(info.model, id, preload: :author) do
check_article_pending(article, user)
end
end

defp check_article_pending(%{pending: @audit_legal} = article, _) do
{:ok, article}
end

defp check_article_pending(%{pending: @audit_failed} = article, _) do
{:ok, article}
end

defp check_article_pending(%{pending: @audit_illegal} = article, %User{id: user_id}) do
case article.author.user_id == user_id do
true -> {:ok, article}
Expand All@@ -571,14 +583,6 @@ defmodule GroupherServer.CMS.Delegate.ArticleCURD do
end
end

# pending article can be seen is viewer is author
defp check_article_pending(thread, id, %User{} = user) do
with {:ok, info} <- match(thread),
{:ok, article} <- ORM.find(info.model, id, preload: :author) do
check_article_pending(article, user)
end
end

defp check_article_pending(%{pending: @audit_illegal}) do
raise_error(:pending, "this article is under audition")
end
Expand Down
2 changes: 1 addition & 1 deletionlib/groupher_server/cms/delegates/hooks/cite.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -48,7 +48,7 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Cite do
@article_threads get_config(:article, :threads)
@valid_article_prefix Enum.map(@article_threads, &"#{@site_host}/#{&1}/")

def handle(%{body: body} = artiment) do
def handle(%{body: body} = artiment)when not is_nil(body)do
with {:ok, %{"blocks" => blocks}} <- Jason.decode(body),
{:ok, artiment} <- preload_author(artiment) do
Multi.new()
Expand Down
2 changes: 1 addition & 1 deletionlib/groupher_server/cms/delegates/hooks/mention.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -17,7 +17,7 @@ defmodule GroupherServer.CMS.Delegate.Hooks.Mention do

@article_mention_class "cdx-mention"

def handle(%{body: body} = artiment) do
def handle(%{body: body} = artiment)when not is_nil(body)do
with {:ok, %{"blocks" => blocks}} <- Jason.decode(body),
{:ok, artiment} <- preload_author(artiment) do
blocks
Expand Down
2 changes: 1 addition & 1 deletionlib/groupher_server/cms/delegates/works_curd.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -151,7 +151,7 @@ defmodule GroupherServer.CMS.Delegate.WorksCURD do
end

defp get_techstack(title) do
case ORM.find_by(Techstack, %{title: title}) do
case ORM.find_by(Techstack, %{raw: title}) do
{:error, _} -> create_techstack(title)
{:ok, techstack} -> {:ok, techstack}
end
Expand Down
4 changes: 2 additions & 2 deletionslib/groupher_server_web/context.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,8 +33,8 @@ defmodule GroupherServerWeb.Context do
with ["Bearer " <> token] <- get_req_header(conn, "authorization"),
{:ok, cur_user} <- authorize(token) do
# IO.inspect(
# RemoteIP.parse(get_req_header(conn, "x-forwarded-for")),
# label: "x-forwarded-for"
#RemoteIP.parse(get_req_header(conn, "x-forwarded-for")),
#label: "#>#x-forwarded-for"
# )

case RemoteIP.parse(get_req_header(conn, "x-forwarded-for")) do
Expand Down
3 changes: 3 additions & 0 deletionslib/groupher_server_web/middleware/github_user.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
defmodule GroupherServerWeb.Middleware.GithubUser do
@moduledoc """
handle github oauth login
"""
@behaviour Absinthe.Middleware

import Helper.Utils, only: [handle_absinthe_error: 2]
Expand Down
5 changes: 2 additions & 3 deletionslib/helper/audit_bot.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,14 +18,13 @@ defmodule Helper.AuditBot do
audit_failed_reason: ""
}
"""
#import Helper.Utils, only: [done: 1]
import Helper.Utils, only: [get_config: 2]

# conclusionType === 1
@conclusionOK 1
@conclusionMaybe 3

# @token "24.4d53f20a8a47348f5a90011bc1a16e84.2592000.1639149221.282335-25148796"
@token "24.4d53f20a8a47348f5a90011bc1a16e84.2592000.1639149221.282335-25148796"
@token get_config(:audit, :token)

@url "https://aip.baidubce.com"
@endpoint "#{@url}/rest/2.0/solution/v1/text_censor/v2/user_defined?access_token=#{@token}"
Expand Down
10 changes: 6 additions & 4 deletionslib/helper/ip_2_city.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -8,7 +8,8 @@ defmodule Helper.IP2City do

@endpoint "https://restapi.amap.com/v3/ip"
@timeout_limit 5000
@service_key get_config(:ip_locate, :ip_service)
@token get_config(:ip_locate, :ip_service)
# @token "52ecdfe2505a4a5a6ff66b2184c10036"

# plug(Tesla.Middleware.BaseUrl, "https://restapi.amap.com/v3/ip")
plug(Tesla.Middleware.Retry, delay: 200, max_retries: 2)
Expand All@@ -31,14 +32,15 @@ defmodule Helper.IP2City do

# http://ip.yqie.com/search.aspx?searchword=%E6%88%90%E9%83%BD%E5%B8%82
def locate_city(ip) do
query = [ip: ip, key: @service_key]
# query = [ip: ip, key: @token]
query = [ip: ip, key: get_config(:ip_locate, :ip_service)]

with true <- Mix.env() !== :test do
case get(@endpoint, query: query) do
%{status: 200, body: %{"city" => city}} ->
{:ok, %Tesla.Env{status: 200, body: %{"city" => city}}} ->
handle_result({:ok, city})

_error ->
error ->
{:error, "error"}
end
else
Expand Down
50 changes: 21 additions & 29 deletionslib/helper/oauth2/github.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -4,44 +4,38 @@ defmodule Helper.OAuth2.Github do

# see Tesla intro: https://medium.com/@teamon/introducing-tesla-the-flexible-http-client-for-elixir-95b699656d88
@timeout_limit 5000

# @client_id get_config(:github_oauth, :client_id)
# @client_secret get_config(:github_oauth, :client_secret)
@redirect_uri get_config(:github_oauth, :redirect_uri)

# wired only this style works
plug(Tesla.Middleware.BaseUrl, "https://github.com/login/oauth")
# plug(Tesla.Middleware.BaseUrl, "https://www.github.com/login/oauth")
# plug(Tesla.Middleware.BaseUrl, "https://api.github.com/login/oauth")
plug(Tesla.Middleware.Headers, [{"User-Agent", "groupher server"}])
# plug(Tesla.Middleware.Headers, %{
# "User-Agent" => "groupher server"
# "Accept" => "application/json"
# "Accept" => "application/json;application/vnd.github.jean-grey-preview+json"
# })

plug(Tesla.Middleware.Retry, delay: 200, max_retries: 2)
@endpoint_token "https://github.com/login/oauth/access_token"
@endpoint_user "https://api.github.com/user"

plug(Tesla.Middleware.Headers, [{"Accept", "application/json"}])

plug(Tesla.Middleware.Retry, delay: 300, max_retries: 2)
plug(Tesla.Middleware.Timeout, timeout: @timeout_limit)
plug(Tesla.Middleware.JSON)
plug(Tesla.Middleware.FormUrlencoded)

def user_profile(code) do
# body = "client_id=#{@client_id}&client_secret=#{@client_secret}&code=#{code}&redirect_uri=#{@redirect_uri}"
# post("access_token?#{body}",%{})
headers = %{"Accept" => "application/json"}

query = [
code: code,
# 不知道是不是 Bug, 如果把这个提出去会导致读取不到。。
client_id: get_config(:github_oauth, :client_id),
client_secret: get_config(:github_oauth, :client_secret),
redirect_uri: @redirect_uri
]

try do
case post("/access_token", %{}, query: query, headers: headers) do
%{status: 200, body: %{"error" => error, "error_description" => description}} ->
ret = post(@endpoint_token, %{}, query: query)

case ret do
{:ok, %Tesla.Env{body: %{"error" => error, "error_description" => description}}} ->
{:error, "#{error}: #{description}"}

%{status: 200, body: %{"access_token" => access_token, "token_type" => "bearer"}} ->
{:ok, %Tesla.Env{status: 200, body: %{"access_token" => access_token}}} ->
user_info(access_token)
end
rescue
Expand All@@ -50,24 +44,22 @@ defmodule Helper.OAuth2.Github do
end
end

def user_info(access_token) do
url = "https://api.github.com/user"
# this special header is too get node_id
# see: https://developer.github.com/v3/

headers = %{"Accept" => "application/vnd.github.jean-grey-preview+json"}
defp user_info(access_token) do
query = [access_token: access_token]
headers = [{"Authorization", "token #{access_token}"}]

try do
case get(url, query: query, headers: headers) do
%{status: 200, body: body} ->
ret = get(@endpoint_user, query: query, headers: headers)

case ret do
{:ok, %Tesla.Env{status: 200, body: body}} ->
body = body |> Map.merge(%{"access_token" => access_token})
{:ok, body}

%{status: 401, body: body} ->
{:ok, %Tesla.Env{status: 401, body: body}} ->
{:error, "OAuth2 Github: " <> body["message"]}

%{status: 403, body: body} ->
{:ok, %Tesla.Env{status: 403, body: body}} ->
{:error, "OAuth2 Github: " <> body}

_ ->
Expand Down
5 changes: 2 additions & 3 deletionslib/helper/plausible.ex
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,12 +13,12 @@ defmodule Helper.Plausible do
@timeout_limit 4000

@site_id "coderplanets.com"
@service_key get_config(:plausible, :token)
@token get_config(:plausible, :token)

@cache_pool :online_status

plug(Tesla.Middleware.BaseUrl, @endpoint)
plug(Tesla.Middleware.Headers, [{"Authorization", "Bearer #{@service_key}"}])
plug(Tesla.Middleware.Headers, [{"Authorization", "Bearer #{@token}"}])
plug(Tesla.Middleware.Retry, delay: 200, max_retries: 2)
plug(Tesla.Middleware.Timeout, timeout: @timeout_limit)
plug(Tesla.Middleware.JSON)
Expand All@@ -40,7 +40,6 @@ defmodule Helper.Plausible do
end
else
error ->
IO.inspect(error, label: "got error")
Cache.put(@cache_pool, :realtime_visitors, 1)
{:ok, 1}
end
Expand Down
3 changes: 2 additions & 1 deletiontest/groupher_server/cms/articles/pending_flag_test.exs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -137,10 +137,11 @@ defmodule GroupherServer.Test.CMS.PostPendingFlag do

alias CMS.Delegate.Hooks

@tag :wip
test "can audit paged audit failed posts", ~m(post_m)a do
{:ok, post} = ORM.find(CMS.Model.Post, post_m.id)

{:ok,_} = CMS.set_article_audit_failed(post, %{})
{:ok,post} = CMS.set_article_audit_failed(post, %{})

{:ok, result} = CMS.paged_audit_failed_articles(:post, %{page: 1, size: 20})
assert result |> is_valid_pagination?(:raw)
Expand Down
46 changes: 23 additions & 23 deletionstest/groupher_server/seeds/community_seed_test.exs
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -14,24 +14,24 @@ defmodule GroupherServer.Test.Seeds.CommunitySeed do

assert community.title == "CoderPlanets"
assert community.raw == "home"
assert found.threads |> length ==6
assert found.threads |> length ==5

threads = found.threads |> Enum.map(& &1.thread.title)
assert threads == ["帖子", "雷达", "博客", "工作", "CPer", "设置"]
assert threads == ["帖子", "雷达", "博客", "工作", "CPer"]
# IO.inspect(found, label: "found --> ")
end

test "blackhole community" do
{:ok, community} = CMS.seed_community(:blackhole)
{:ok, found} = ORM.find(Community, community.id, preload: [threads: :thread])
#test "blackhole community" do
# {:ok, community} = CMS.seed_community(:blackhole)
# {:ok, found} = ORM.find(Community, community.id, preload: [threads: :thread])

assert community.title == "黑洞"
assert community.raw == "blackhole"
assert found.threads |> length == 2
# assert community.title == "黑洞"
# assert community.raw == "blackhole"
# assert found.threads |> length == 2

threads = found.threads |> Enum.map(& &1.thread.title)
assert threads == ["帖子", "账户"]
end
# threads = found.threads |> Enum.map(& &1.thread.title)
# assert threads == ["帖子", "账户"]
#end

test "Feedback community" do
{:ok, community} = CMS.seed_community(:feedback)
Expand DownExpand Up@@ -106,13 +106,13 @@ defmodule GroupherServer.Test.Seeds.CommunitySeed do
{:ok, tags} = CMS.paged_article_tags(filter)
tags_titles = tags |> Enum.map(& &1.title)

assert tags_titles == ["求助", "讨论", "推荐", "小聚", "其他"]
assert tags_titles == ["求助", "分享推荐", "讨论", "学习资源", "小聚", "杂谈", "社区事务"]

assert community.title == "elixir"
assert community.raw == "elixir"

threads = found.threads |> Enum.map(& &1.thread.title)
assert threads == ["帖子", "雷达", "博客", "101", "awesome", "作品", "工作", "分布", "设置"]
#threads = found.threads |> Enum.map(& &1.thread.title)
#assert threads == ["帖子", "雷达", "博客", "101", "awesome", "作品", "工作", "分布", "设置"]
end

test "can seed multi lang communities" do
Expand All@@ -123,15 +123,15 @@ defmodule GroupherServer.Test.Seeds.CommunitySeed do
# assert communities.total_count == 9
radom_community = communities.entries |> Enum.random()
{:ok, found} = ORM.find(Community, radom_community.id, preload: [threads: :thread])
assert length(found.threads) ==9
assert length(found.threads) ==6

filter = %{community_id: radom_community.id, thread: "POST"}
{:ok, tags} = CMS.paged_article_tags(filter)
tags_titles = tags |> Enum.map(& &1.title)
assert tags_titles == ["求助", "讨论", "推荐", "小聚", "其他"]
#filter = %{community_id: radom_community.id, thread: "POST"}
#{:ok, tags} = CMS.paged_article_tags(filter)
#tags_titles = tags |> Enum.map(& &1.title)
#assert tags_titles == ["求助", "讨论", "推荐", "小聚", "其他"]

threads = found.threads |> Enum.map(& &1.thread.title)
assert threads == ["帖子", "雷达", "博客", "101", "awesome", "作品", "工作", "分布", "设置"]
#threads = found.threads |> Enum.map(& &1.thread.title)
#assert threads == ["帖子", "雷达", "博客", "101", "awesome", "作品", "工作", "分布", "设置"]
end

test "can seed a general framework community" do
Expand All@@ -142,13 +142,13 @@ defmodule GroupherServer.Test.Seeds.CommunitySeed do
{:ok, tags} = CMS.paged_article_tags(filter)
tags_titles = tags |> Enum.map(& &1.title)

assert tags_titles == ["求助", "讨论", "推荐", "小聚", "其他"]
assert tags_titles == ["求助", "分享推荐", "讨论", "学习资源", "小聚", "杂谈", "社区事务"]

assert community.title == "react"
assert community.raw == "react"

threads = found.threads |> Enum.map(& &1.thread.title)
assert threads == ["帖子", "雷达", "博客", "101", "awesome", "作品", "工作", "分布", "设置"]
assert threads == ["帖子", "雷达", "博客", "作品", "工作", "分布"]
end
end
end
Loading

[8]ページ先頭

©2009-2025 Movatter.jp